On 9/29/16 11:49 PM, Martijn Dekker wrote:
> I detected an oddity (possible bug) in bash: the usual optimisation for
> launching external processes in simple command substitutions is turned
> off while executing a dot script.
> 
> Background: For reasons that would take too much space to explain here,
> I need a cross-platform/POSIX way to get the process ID of the current
> subshell. I can't use $BASHPID for this as it's only available on newer
> bash (not including the default bash on Mac OS X).
> 
> As far as I know, the canonical cross-platform way of detecting the PID
> of a subshell is:
> 
>       my_subshell_pid=$(sh -c 'echo $PPID')
> 
> This works fine on every shell, except on bash when a dot script is
> being executed.
This isn't a bug.  It can be viewed as an opportunity for further
optimization.  Rather than assume an implicit `exec', make your
assumption explicit and use something like `exec sh -c 'echo $PPID''.

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    c...@case.edu    http://cnswww.cns.cwru.edu/~chet/

Reply via email to