How to Find Out The User of Parent Shell Inside a Child Shell

How to find out the user of parent shell inside a child shell?

You can use the $PPID variable to assist you along with a command or two:

#!/bin/bash
USER=`ps u -p $PPID | awk '{print $1}'|tail -1`
echo $USER

How to find out the user of parent shell inside a child shell?

You can use the $PPID variable to assist you along with a command or two:

#!/bin/bash
USER=`ps u -p $PPID | awk '{print $1}'|tail -1`
echo $USER

How can I tell if I'm in a child shell

Use the SHLVL environment variable.

man bash:

SHLVL : Incremented by one each time an instance of bash is started.

$ echo "$SHLV"
1

$ bash

$ echo "$SHLV"
2

$ exit

$ echo "$SHLV"
1

How to tell the parent-child relationship of shell processes?

If all you are given is two process IDs, there is no way to tell which is the parent and which is the child, or if one is even an ancestor of the other. Process IDs are assigned in order as processes are created, using the next largest (modulo 65535) unused ID available at the time.

You can, however, examine the value of PPID in the shell to see who the parent process is.

bash-4.3$ echo $$
45564
bash-4.3$ bash
bash-4.3$ echo $$
45565
bash-4.3$ echo $PPID
45564
bash-4.3$

How to pass values from child shell to parent shell

This is impossible. The best that can be done is for the child to print the desired assignments, and for the parent to exec them.

exit from a function in child shell script to parent shell script

You can't specify how many levels "up" to return, but you can respond to the exit status of the call. Just let status return as it currently does, and since you are sourcing the file, change the call to status to

status || return

This will cause the sourced script to return to the parent if status has a non-zero exit status.

How to get child process from parent process

Just use :

pgrep -P $your_process1_pid


Related Topics



Leave a reply



Submit