bug-bash
[Top][All Lists]
Advanced

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

Re: bash 3.2.51, ERR traps and subshells


From: Chet Ramey
Subject: Re: bash 3.2.51, ERR traps and subshells
Date: Tue, 22 Jun 2010 14:51:17 -0400
User-agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.1.7) Gecko/20100111 Lightning/1.0b1 Thunderbird/3.0.1

On 6/22/10 12:51 AM, Andres P wrote:
> Bash 4.1 does not set the ERR trap:

It does, but remember that the ERR trap is only executed under the
circumstances that would cause the shell to exit when set -e is enabled.

> 
> $ env -i HOME="$HOME" TERM="$TERM" bash3 <<\!
> 
>  set -o errexit
>  set -o errtrace
> 
>  TRIGGERED_ERR() { return $?; }
> 
>  trap 'TRIGGERED_ERR' ERR
> 
>  set -o xtrace
> 
>  var=$(false) || true
>  echo $?
> 
>  var=$(false || true) # only way of not triggering it...
>  echo $?
> 
> !
> 
> ++ false           # Subshell false
> +++ TRIGGERED_ERR  # Ignores outer "|| true"
> +++ return 1
> + var=
> + true
> + echo 0
> 0                  # But the entire command line does
>                   # not set off errexit
> ++ false
> ++ true            # Predictable second subshell...
> + var=
> + echo 0
> 0
> 
> 
> Before I write a patch, is this bug documented? I could not find it in
> the archives.
> 
> Is it a bug at all or is it expected behaviour?

One of the changes between bash-3.2 and bash-4.0 was to make command
substitution understand when it was being executed in a context where
the return value would be ignored, and to ignore any inherited setting
of -e and the ERR trap in those cases.  This was part of the big change
in the behavior of set -e in response to the consensus of the Posix
working group, and more directly the result of a bug report.

> The ERR trap is not executed if the failed command is part of the command
> list immediately  following  a while  or until keyword, part of the test in an
> if statement, part of a && or ⎪⎪ list, or if the command's return value is
> being inverted via !

The exit status of the command substitution will ultimately be ignored
because it's part of an assignment statement on the LHS of a || list, so
the commands run in the command substitution inherit that state.

Chet


-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU    chet@case.edu    http://cnswww.cns.cwru.edu/~chet/



reply via email to

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