bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Command substitution optimisation in dot scripts


From: Chet Ramey
Subject: Re: Command substitution optimisation in dot scripts
Date: Fri, 30 Sep 2016 09:21:42 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:45.0) Gecko/20100101 Thunderbird/45.3.0

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    chet@case.edu    http://cnswww.cns.cwru.edu/~chet/



reply via email to

[Prev in Thread] Current Thread [Next in Thread]