[Top][All Lists]

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

Re: EXIT trap in implicit vs. explicit pipeline subshell

From: Chet Ramey
Subject: Re: EXIT trap in implicit vs. explicit pipeline subshell
Date: Tue, 09 Jun 2015 19:39:10 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:31.0) Gecko/20100101 Thunderbird/31.6.0

Hash: SHA1

On 6/7/15 12:55 PM, Miroslav Koskar wrote:

> Bash Version: 4.3
> Patch Level: 39
> Release Status: release
> Description:
>     For lack of better terminology I call subshell introduced by pipeline
>     as implicit when written like this:
>         echo 'This will run in subshell' | cat
>     and explicit when written like this:
>         ( echo 'This will run in subhshell' ) | cat
>     One would expect above snippets of code to be equivalent in function,
>     in second one parentheses are unnecessary, because pipeline will run
>     every part in subshell (unless "lastpipe" is set, which is of no inte
>     for us here).
>     I've observed that it's actually not true for traps, namely EXIT trap
>     If EXIT trap is set in parent (main) shell, then this is inherited in
>     subshell in implicit case. It can be observed by running `trap` or `t
rap -p`
>     in subshell. This inherited trap is actually not executed on exit fro
>     subshell. In explicit case, EXIT trap is rightly not inherited.

Close.  What you call the `implicit' and `explicit' pipelines (subshells)
actually  both inherit the trap strings, but not the actual signal
dispositions.  Posix says this:

"The trap command with no operands shall write to standard output a list
of commands associated with each condition. If the command is executed in
a subshell, the implementation does not perform the optional check
described above for a command substitution containing only a single trap
command, and no trap commands with operands have been executed since entry
to the subshell, the list shall contain the commands that were associated
with each condition immediately before the subshell environment was
entered. Otherwise, the list shall contain the commands currently
associated with each condition."

So the idea is that the output of `trap' can be piped.  This came in in
Posix-2008 and bash-4.2.

One exception to this, as you discovered, is that bash clears out the EXIT
trap's trap string when a (...) subshell is executed.  This is one of the
things bash does for sh compatibility (it goes back to bash-2.0), but it's
probably not worth it any more.

>     Going further, if subshell in implicit case will set it's own EXIT tr
>     that one will not be executed, as oppose to explicit case where it ex

Yes, pipelines don't execute the EXIT trap; subshell commands do.  All
Posix shells do this.  Even the Bourne shell did.

Subshells started to run group commands also inherit trap strings and run
the exit trap.


- -- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU    address@hidden    http://cnswww.cns.cwru.edu/~chet/
Version: GnuPG v2
Comment: GPGTools - http://gpgtools.org


reply via email to

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