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: Miroslav Koskar
Subject: Re: EXIT trap in implicit vs. explicit pipeline subshell
Date: Thu, 11 Jun 2015 10:08:22 +0200

On Jun 09 07:39, Chet Ramey wrote:
> 
> 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.

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"?
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.

> 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.

Is this specific to EXIT trap? I'm not sure I understand original
intention of that, but less exceptions better IMHO.

> 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.

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 ( ... ).

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?

Oh, and this behavior to be described in bash(1) in short wouldn't be
bad idea too I think.

Thanks,
Miro

-- 
Miroslav Koskar
http://mkoskar.com/

Attachment: signature.asc
Description: PGP signature


reply via email to

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