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: Stephane Chazelas
Subject: Re: Command substitution optimisation in dot scripts
Date: Fri, 30 Sep 2016 12:43:38 +0100
User-agent: Mutt/1.5.21 (2010-09-15)

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



reply via email to

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