bug-bash
[Top][All Lists]
Advanced

[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: Thu, 11 Jun 2015 21:35:54 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:31.0) Gecko/20100101 Thunderbird/31.6.0

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 6/11/15 4:08 AM, Miroslav Koskar wrote:

>> So the idea is that the output of `trap' can be piped.  This came in in
>> Posix-2008 and bash-4.2.
> 
> I've forgot to check POSIX for this, that's a good point. So to get
> list of traps corresponding to subshell, some has to get assigned e.g.,
> this would work (listing no traps):
> 
> #!/bin/bash
> trap 'echo EXIT-$BASH_SUBSHELL' EXIT
> { trap - ERR; trap; } | cat
> 
> Wouldn't it be beneficial to introduce say an option to a `trap` command
> with meaning of "print traps corresponding to current shell/subshell"?

Maybe, but there's no demand for it.  There is an easy workaround.  If
you want to make sure that workaround has no side effects, try adding a
trap for a signal like SIGKILL, which can't be caught, blocked, or ignored.

> More complexity ... probably, but more clarity too I guess. I wonder if
> mentioning this tricky bit in bash(1), maybe with just reference to
> POSIX would be of value.

I'll look at some appropriate text for the man page and info doc.

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

Yes, as I said.

> I'm not sure I understand original
> intention of that, but less exceptions better IMHO.

Sure.  The original intent of the code has been lost in the 20+ years
since, but I'm fairly sure of the sh compatibility thing, since the
Bourne shell never inherited the exit trap like that.

>> 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 ru
n
>> the exit trap.
> 
> So in other words, if I have an EXIT trap defined say in a function
> running as a part of the pipeline (and want to fire that trap), I have
> to scope it in explicit subshell like ( ... ).

Correct.  No shell runs an exit trap in a pipeline element.

> In fact group command { ... } would work too, as I've showed and you've
> confirmed. One last bit here I guess, why is it that the $BASH_SUBSHELL,
> would not reflect that is it in fact running in subshell?

BASH_SUBSHELL measures (...) subshells, not pipeline elements.  For
example, the following lines show that it has value `1':

( echo in subshell: $BASH_SUBSHELL )
( echo in subshell pipeline: $BASH_SUBSHELL ) | cat

- -- 
``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/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2
Comment: GPGTools - http://gpgtools.org

iEYEARECAAYFAlV6N3IACgkQu1hp8GTqdKtjeACfdZnjcZZL6gDauEdskY9Tthl3
IU8AoIafGmVHa9o1/GPDoK0iy8Obzpur
=079z
-----END PGP SIGNATURE-----



reply via email to

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