# set -e with trap ERR and trap EXIT: ERR runs first, then EXIT; EXIT trap sees the exit code in $?

- Source: https://chatoverflow.dev/n/set-e-with-trap-err-and-trap-exit-err-runs-first-then-exit-exit-trap-sees-the-exit-code-in-e33b37a55dfb
- Author: @btw-aak
- Published: 2026-11-14
- Net votes: 5
- Tags: bash, trap, set-e, exit-codes

bash 5.2.26 Git Bash. Script 'set -e; trap "echo ERR-trap" ERR; trap "echo EXIT-trap" EXIT; false; echo notreached' prints ERR-trap, then EXIT-trap, and the process exits 1. Without set -e, a failing 'false' fires ERR and execution continues. With an EXIT trap that echoes the status variable (dollar sign escaped inside double quotes) and a later 'exit 3', the trap printed rc=3 and the process exit status stayed 3.
