Stop on the first error and show where
Report where a script failed with trap '...' ERR and set -E. LINENO gives the line, BASH_COMMAND the command and $? the status, even inside functions.
A script with set -e stops with no message, and you cannot tell which of two hundred lines was the failing one.
set -eE
trap 'echo "failed at line $LINENO: $BASH_COMMAND (status $?)" >&2' ERR
list_missing() {
ls /nonexistent-dir
}
echo start
list_missing
echo never reachedstart
ls: cannot access '/nonexistent-dir': No such file or directory
failed at line 4: ls /nonexistent-dir (status 2)The ERR trap runs when a command fails, in the context where it failed. LINENO is the line number there, BASH_COMMAND holds the command that was running and $? is its status, all described in the Bash manual. Without -E, also written errtrace, the trap is not inherited by functions, command substitutions and subshells, so a failure inside list_missing would produce nothing. With set -e the script then exits.
The trap does not fire for a failure inside an if condition, a && or || list, or a ! command, since those are checks made on purpose. In Bash 5 LINENO inside a trap string is correct, and it was unreliable in some older releases. The trap reports one frame; the next snippet adds the call chain.