Discussion:
trap ERR in || with set -E enabled
(too old to reply)
Peng Yu
2018-12-05 17:50:54 UTC
Permalink
Hi,

I'd expect that `false` below is trapped independent from whether `f `
is used with `||` or not. But it doesn't do so.

The manual does not discuss the set -E case explicitly, but I think
that it is better not to change the behavior of trap ERR in nonlocal
places. Is this a design issue that worth reconsidering? Thanks.

$ cat main.sh
set -E
trap 'echo in ERR trap' ERR
function f {
echo BEGIN
false
echo END
}

f
f || true
echo xxx

set -v
false || true
$ ./main.sh
BEGIN
in ERR trap
END
BEGIN
END
xxx
false || true

====
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 command executed in a && or
|| list except the command following the final && or ||, any command
in a pipeline but the last, or if the command's return value is
being inverted using !. These are the same conditions obeyed by the
errexit (-e) option.
--
Regards,
Peng
Chet Ramey
2018-12-05 18:39:51 UTC
Permalink
Post by Peng Yu
Hi,
I'd expect that `false` below is trapped independent from whether `f `
is used with `||` or not. But it doesn't do so.
The manual does not discuss the set -E case explicitly, but I think
that it is better not to change the behavior of trap ERR in nonlocal
places. Is this a design issue that worth reconsidering? Thanks.
The ERR trap fires in exactly the same circumstances where `set -e' would
cause the shell to exit. The converse is also true: it doesn't fire in the
cases where `set -e' has no effect. That is the primary design issue.
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU ***@case.edu http://tiswww.cwru.edu/~chet/
Loading...