3

As far as I know

echo $$

shows the PID of the current BASH I am in.

I do not know if the expasnsion $(...command...) runs the command in a subshell or the current shell, however

echo $(echo $$)

shows the same PID But, as for (...command...) I was very sure, that the command is run inside a subshell and therefore in a shell with another PID than the current shell, but

(echo $$)

shows also the same PID

So, in all three cases the same PID,namely 1771, was displayed. I am confused, based on what I learned so far, I would not have expected it. Why?

sharkant
  • 3,560
  • 10
  • 30
  • 46
  • 2
    [Try `echo $BASHPID`](https://www.gnu.org/software/bash/manual/bashref.html#index-BASHPID). – Michael Homer May 16 '17 at 07:01
  • This is what I found inside the manual for bash: – sharkant May 16 '17 at 08:19
  • $ Expands to the process ID of the shell. In a () subshell, it expands to the process ID of the current shell, not the subshell. – sharkant May 16 '17 at 08:19
  • now I understand why people say RTFM – sharkant May 16 '17 at 08:20
  • for those who do not know: in $$ , the second $ is the actual special character for the PID and the first $ tells the bash that the following character is to be understood as a special character – sharkant May 16 '17 at 08:22
  • Thanks! 4 years old and this is exactly the RTFM I was looking for! But how do I get the calling subshell pid? Like $$PPID but for the calling subshell not the calling process that invoked the shell? – Christopher King May 19 '21 at 21:25

0 Answers0