Am 04. Feb, 2016 schwätzte der.hans so: moin moin, for completeness... you made me reach to the Advanced Bash-Scripting Guide to remember the variable name :). Notice the values of $BASHPID in a COMMAND EXECUTION ENVIRONMENT vs in a true subshell. http://www.tldp.org/LDP/abs/html/internalvariables.html#BASHPIDREF :) lufthans@anke:~$ cat fred.sh function backGround() { echo "Pid $1 is $$ $BASHPID $SHLVL" sleep 3 } function backGround_subshell() { echo "echo Pid $1 is $$ $BASHPID $SHLVL" sleep 3 } echo "My pid is $$ $BASHPID $SHLVL" backGround 1 & echo "BPid 1 is $!" ps -ef | grep $! (backGround_subshell 4) & echo "BPid 4 is $!" ps -ef | grep $! bash -c 'echo true subshell PID $$ $BASHPID $SHLVL; ps -ef | grep $$' wait echo :) lufthans@anke:~$ :) lufthans@anke:~$ bash fred.sh anke My pid is 27811 27811 2 BPid 1 is 27812 Pid 1 is 27811 27812 2 lufthans 27812 27811 0 21:34 pts/0 00:00:00 bash fred.sh anke lufthans 27814 27811 0 21:34 pts/0 00:00:00 grep 27812 lufthans 27815 27812 0 21:34 pts/0 00:00:00 sleep 3 BPid 4 is 27816 echo Pid 4 is 27811 27816 2 lufthans 27816 27811 0 21:34 pts/0 00:00:00 bash fred.sh anke lufthans 27818 27811 0 21:34 pts/0 00:00:00 grep 27816 lufthans 27819 27816 0 21:34 pts/0 00:00:00 sleep 3 true subshell PID 27820 27820 3 lufthans 27820 27811 0 21:34 pts/0 00:00:00 bash -c echo true subshell PID $$ $BASHPID $SHLVL; ps -ef | grep $$ lufthans 27821 27820 0 21:34 pts/0 00:00:00 ps -ef lufthans 27822 27820 0 21:34 pts/0 00:00:00 grep 27820 :) lufthans@anke:~$ ciao, der.hans > Am 04. Feb, 2016 schwätzte Bryan O'Neal so: > > moin moin, > > $( ) is command redirection. > > ( ) is a subshell, sort of. > > Here are some excerpts from the bash man page: > > ### > (list) list is executed in a subshell environment (see COMMAND EXECU > TION ENVIRONMENT below). > > COMMAND EXECUTION ENVIRONMENT > The shell has an execution environment, which consists of the > follow > ing: > . > . > . > · various process IDs, including those of background > jobs, the > value of $$, and the value of PPID > ### > > If you want a full subshell, invoke bash and create another process. Also, > check the values of SHLVL. > > :) lufthans@fred:~$ cat fred.sh function backGround() { > echo "Pid $1 is $$ $SHLVL" > sleep 3 > } > function backGround_subshell() { > echo "echo Pid $1 is $$ $SHLVL" > sleep 3 > } > echo "My pid is $$ $SHLVL" > backGround 1 & > echo "BPid 1 is $!" > ps -ef | grep $! > (backGround_subshell 4) & > echo "BPid 4 is $!" > ps -ef | grep $! > bash -c 'echo true subshell PID $$ $SHLVL; ps -ef | grep $$' > wait > echo > :) lufthans@fred:~$ > > :) lufthans@fred:~$ bash fred.sh anke > My pid is 27695 2 > BPid 1 is 27696 > Pid 1 is 27695 2 > lufthans 27696 27695 0 21:26 pts/0 00:00:00 bash fred.sh anke > lufthans 27698 27695 0 21:26 pts/0 00:00:00 grep 27696 > lufthans 27699 27696 0 21:26 pts/0 00:00:00 sleep 3 > BPid 4 is 27700 > echo Pid 4 is 27695 2 > lufthans 27700 27695 0 21:26 pts/0 00:00:00 bash fred.sh anke > lufthans 27702 27695 0 21:26 pts/0 00:00:00 grep 27700 > true subshell PID 27704 3 > lufthans 27704 27695 0 21:26 pts/0 00:00:00 bash -c echo true subshell > PID $$ $SHLVL; ps -ef | grep $$ > lufthans 27705 27704 0 21:26 pts/0 00:00:00 ps -ef > lufthans 27706 27704 0 21:26 pts/0 00:00:00 grep 27704 > > :) lufthans@fred:~$ > > ciao, > > der.hans > >> Sorry for the cross post but this is not an easy question I don't think.... >> >> Given: >> Pid of a process is $$ >> Pid of a child process just spawned is $! >> However a child process does not see $$ as it's pid but rather the pid of >> it's parent... How can the child process know it's own pid? >> >> Example: >> [root@mybox l]# cat backTest >> function backGround() { >> echo "Pid $1 is $$" >> } >> function backGround_subshell() { >> echo "echo Pid $1 is $$" >> } >> echo "My pid is $$" >> backGround 1 & >> echo "BPid 1 is $!" >> backGround 2 & >> echo "BPid 2 is $!" >> backGround 3 & >> echo "BPid 3 is $!" >> $(backGround_subshell 4) & >> echo "BPid 4 is $!" >> $(backGround_subshell 5) & >> echo "BPid 5 is $!" >> $(backGround_subshell 6) & >> echo "BPid 6 is $!" >> wait >> echo >> [root@mybox l]# bash backTest >> My pid is 16712 >> BPid 1 is 16713 >> Pid 1 is 16712 >> BPid 2 is 16714 >> Pid 2 is 16712 >> BPid 3 is 16715 >> BPid 4 is 16716 >> BPid 5 is 16717 >> BPid 6 is 16719 >> Pid 4 is 16712 >> Pid 3 is 16712 >> Pid 6 is 16712 >> Pid 5 is 16712 >> > > -- # http://www.LuftHans.com/ http://www.PhxLinux.org/ # "We should not be building surveillance technology into standards. # Law enforcement was not supposed to be easy. # Where it is easy, it's called a police state." -- Jeff Schiller