I have a simple script :

set -e
trap "echo BOO!" ERR 

function func(){
    ls /root/


I would like to trap ERR if my script fails (as it will here b/c I do not have the permissions to look into /root). However, when using set -e it is not trapped. Without set -e ERR is trapped.

According to the bash man page, for set -e :

... A trap on ERR, if set, is executed before the shell exits. ...

Why isn't my trap executed? From the man page it seems like it should.

  • 2
    As an aside: it's better to single-quote your trap handlers, unless you explicitly want variable references in it expanded up front. – mklement0 Mar 4 '16 at 17:22

chepner's answer is the best solution: If you want to combine set -e (same as: set -o errexit) with an ERR trap, also use set -o errtrace (same as: set -E).

In short: use set -eE in lieu of just set -e:


set -eE  # same as: `set -o errexit -o errtrace`
trap 'echo BOO!' ERR 

function func(){
  ls /root/

# Thanks to -E / -o errtrace, this still triggers the trap, 
# even though the failure occurs *inside the function*.

A more sophisticated example trap example that prints the message in red and also prints the exit code:
trap 'printf "\e[31m%s: %s\e[m\n" "BOO!" $?' ERR

man bash says about set -o errtrace / set -E:


What I believe is happening:

  • Without -e: The ls command fails inside your function, and, due to being the last command in the function, the function reports ls's nonzero exit code to the caller, your top-level script scope. In that scope, the ERR trap is in effect, and it is invoked (but note that execution will continue, unless you explicitly call exit from the trap).

  • With -e (but without -E): The ls command fails inside your function, and because set -e is in effect, Bash instantly exits, directly from the function scope - and since there is no ERR trap in effect there (because it wasn't inherited from the parent scope), your trap is not called.

While the man page is not incorrect, I agree that this behavior is not exactly obvious - you have to infer it.

| improve this answer | |
  • Thanks, I will. Also instead of ls /root/ when I run bash try.sh which has #!/bin/bash exit 1, it does not catched by the trap, is there any way to catch the called script if it is exited ? – alper Jul 2 at 18:38
  • 1
    @alper There's a separate EXIT trap invoked when the script exits, for whatever reason, and whether successfully or not. To only act on non-zero exit codes, you must check $?; e.g.: trap 'ec=$?; (( ec != 0 )) && echo "Exited with failure: $ec"' EXIT – mklement0 Jul 3 at 13:18

You need to use set -o errtrace for the function to inherit the trap.

| improve this answer | |
  • Not necessarily, if you use shopt -s extdebug. – jarno Nov 7 '19 at 7:57

Replace ERR with EXIT and it will work.

The syntax of the trap command is: trap [COMMANDS] [SIGNALS]

For more info, please read http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_12_02.html

| improve this answer | |
  • 12
    While this is a viable workaround (albeit not needed, if set -o errtrace is used), you should mention that EXIT is also called in the event of successful termination, so you'd need to add a conditional such as if [[ $? -ne 0 ]] … to the handler. – mklement0 Mar 4 '16 at 17:12

Your Answer

