TT#119602 Properly handle trap also in case of errors in functions

Quoting from "man bash" about `-E` (AKA errtrace):

 | If set, any trap on ERR is inherited by shell functions, command
 | substitutions, and commands executed in a subshell environment.
 | The ERR trap is normally not inherited in such cases.

To demonstrate the problem see this short shell script:

| % cat foo
| set -eu -o pipefail
|
| bailout() {
|   echo "Bailing out because of error" >&2
|   exit 1
| }
| trap bailout 1 2 3 6 9 14 15 ERR
|
| foo() {
|   echo "Executing magic"
|   magic
| }
|
| foo
| echo end

If "magic" can't be executed, then this fails as follows:

| % bash ./foo
| Executing magic
| ./foo: line 11: magic: command not found

But it doesn't invoke the bailout function via trap.

When using `set -eE` (AKA errexit + errtrace), instead of only
`set -e` (errexit), then it behaves as expected though:

| % bash ./foo
| Executing magic
| ./foo: line 11: magic: command not found
| Bailing out because of error

Change-Id: I26396b87d4a391a75997c061e866709daa57870e
mr9.4.1
Michael Prokop 5 years ago
parent 097464c6f5
commit 8a54cd1374

@ -3,6 +3,7 @@
################################################################################
set -e
set -E
# Functions

Loading…
Cancel
Save