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: I3feca2af2276cd64cd17f8768eb9bf130e170f92
mr9.4
Michael Prokop 5 years ago
parent ae580e527f
commit ae34398b3a

@ -1,6 +1,7 @@
#!/bin/bash
set -e
set -E
declare -r ME="$(basename "$0")"
declare -r OPTS=("$@")

Loading…
Cancel
Save