2016-09-30 04:49:33 +0100, Martijn Dekker: [...] > my_subshell_pid=$(sh -c 'echo $PPID') > > This works fine on every shell, except on bash when a dot script is > being executed. [...]
While it does look like a bug, you could always do: my_subshell_pid=$(exec sh -c 'echo $PPID') To be sure you know where you stand. bash or other shells could have issues if there's an EXIT trap in place that propagates to subshells or any other reason that prevents them from optimising out the fork. Note that it's the same in: $ bash -c 'p=$(sh -c "echo \"\$PPID\""); echo "$p $BASHPID"' 1513 1512 $ bash -c 'p=$(exec sh -c "echo \"\$PPID\""); echo "$p $BASHPID"' 1515 1515 -- Stephane