Skip to main content
Post Made Community Wiki
Source Link
glenn jackman
  • 88.6k
  • 16
  • 124
  • 180

Not directly relevant as you're not asking about traps, but...

ERR traps and set -E vs set -e

Note that set -E does not include set -e. Given this script that has an error in a function:

$ cat trap_test.sh #!/usr/bin/env bash trap 'echo "Error on line $LINENO. Exit code: $?" >&2' ERR myfunc() { noSuchCommand echo OK } myfunc 

then we can examine the combinations of -E and -e

  1. neither specified: no trap, no early exit
    $ bash trap_test.sh; echo "exit status: $?" trap_test.sh: line 6: noSuchCommand: command not found OK exit status: 0 
  2. -e: early exit, no trap from the function
    $ bash -e trap_test.sh; echo "exit status: $?" trap_test.sh: line 6: noSuchCommand: command not found exit status: 127 
  3. -E, trap fired from error in function
    $ bash -E trap_test.sh; echo "exit status: $?" trap_test.sh: line 6: noSuchCommand: command not found Error on line 6. Exit code: 127 OK exit status: 0 
  4. both early exit and trap/
    $ bash -eE trap_test.sh; echo "exit status: $?" trap_test.sh: line 6: noSuchCommand: command not found Error on line 6. Exit code: 127 exit status: 127