$$ in a script vs $$ in a subshell

2019-01-09 13:09发布

问题:

$$ gives process id of the script process when used in a script, like this:

Example 1

#!/bin/bash
# processid.sh
# print process ids

ps -o cmd,pid,ppid
echo "The value of \$\$ is $$"

$ ./processid.sh 
CMD                           PID  PPID
bash                        15073  4657
/bin/bash ./processid.sh    15326 15073
ps -o cmd,pid,ppid          15327 15326
The value of $$ is 15326

Observe the pid given by $$ and ps is 15326

My shell prompt is pid 15073

But in a subshell, $$ gives pid of parent shell (which is 15073)

Example 2

$ ( ps -o cmd,pid,ppid ; echo $$ )
CMD                           PID  PPID
bash                        15073  4657
bash                        15340 15073
ps -o cmd,pid,ppid          15341 15340
15073

Here subshell is pid 15340

Question: Why so? Isn't the script also running in a subshell? What's the difference between the subshell in example 2 and the shell in which the script runs in example 1?

回答1:

I tried and escaping (to pass the $$ to the subshell) does not work as the subshell inherits the $$ value from the parent bash. The solution to this is to use $BASHPID.

(echo $$; echo $BASHPID)

prints the PID from the parent shell and from the subshell.



回答2:

From the bash manpage:

   $      Expands  to  the  process ID of the shell.  In a () subshell, it
          expands to the process ID of the current  shell,  not  the  sub-
          shell.


回答3:

The replacement takes place in the parent shell; the subshell hasn't been started by the time the substitution takes place.



回答4:

A more portable way, linux-only, but also compatible with dash:

read -r my_pid _ < /proc/self/stat
echo $my_pid