A nightly job copies camera clips into a backup folder. The script uses set -e, and to make failures easier to read in the cron log I added an ERR trap that prints the failing line and command. The idea was that when the script stops, the log would say exactly where.
In the run below, the second clip was already gone before the copy reached it. The script stopped, as it should. The trap printed nothing:
#!/usr/bin/env bash
set -e
trap 'echo "ERR: status $? at line $LINENO: $BASH_COMMAND" >&2' ERR
upload() {
cp "clips/$1" backup/
echo "uploaded $1"
}
mkdir -p backup
upload clip_01.mp4
upload clip_02.mp4
echo "all uploaded"
$ bash upload.sh; echo "exit status: $?"
uploaded clip_01.mp4
cp: cannot stat 'clips/clip_02.mp4': No such file or directory
exit status: 1
The exit status is 1 and the cp error is on the terminal, so set -e did its job. The ERR: line I added for exactly this moment did not appear. In a cron job that sends error output to a log, all that is left is the cp message, with no line number, which is what the trap was meant to supply.
The cause is that the failure happened inside a function, and a Bash function does not get the ERR trap unless you ask for it.
The option that passes the trap down
The Bash manual describes the -E option of set (long name errtrace) this way: "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."
Changing set -e to set -eE is all that changed in this version:
#!/usr/bin/env bash
set -eE
trap 'echo "ERR: status $? at line $LINENO: $BASH_COMMAND" >&2' ERR
upload() {
cp "clips/$1" backup/
echo "uploaded $1"
}
mkdir -p backup
upload clip_01.mp4
upload clip_02.mp4
echo "all uploaded"
$ bash upload_E.sh; echo "exit status: $?"
uploaded clip_01.mp4
cp: cannot stat 'clips/clip_02.mp4': No such file or directory
ERR: status 1 at line 6: cp "clips/$1" backup/
exit status: 1
Now the trap runs inside upload, names line 6 and shows the exact cp command that failed, and the script still exits 1.
The first run looks odd next to another sentence on the same manual page, in the description of -e: "A trap on ERR, if set, is executed before the shell exits." In the first script a trap was set, and the shell did exit. What I saw is consistent with the trap simply not existing inside the function body: set -e acted on the failed cp while the shell was running upload, where no ERR trap was in place, and the script ended there. The trap near the start of the file was not consulted. I am describing what the runs showed, not quoting the manual on that order of events.
Without set -e the failure goes quiet
Many report scripts do not use set -e at all and rely on the trap alone to flag problems. Here is a function that reads a clip's size with stat:
#!/usr/bin/env bash
trap 'echo "ERR: status $? at line $LINENO: $BASH_COMMAND" >&2' ERR
report() {
bytes=$(stat -c %s "clips/$1")
echo "$1 size: $bytes"
}
report clip_01.mp4
report clip_02.mp4
echo "report finished"
$ bash report.sh; echo "exit status: $?"
clip_01.mp4 size: 1
stat: cannot statx 'clips/clip_02.mp4': No such file or directory
clip_02.mp4 size:
report finished
exit status: 0
No trap output and exit status 0. The failed stat sits inside a command substitution inside a function, two places the trap is not inherited. The function itself returns the status of its last command, echo, which succeeds, so at the top level nothing failed either. Trouble shows up as an empty size field and nothing else.
With set -E added, the same script reports the problem twice:
#!/usr/bin/env bash
set -E
trap 'echo "ERR: status $? at line $LINENO: $BASH_COMMAND" >&2' ERR
report() {
bytes=$(stat -c %s "clips/$1")
echo "$1 size: $bytes"
}
report clip_01.mp4
report clip_02.mp4
echo "report finished"
$ bash report_E.sh; echo "exit status: $?"
clip_01.mp4 size: 1
stat: cannot statx 'clips/clip_02.mp4': No such file or directory
ERR: status 1 at line 6: stat -c %s "clips/$1"
ERR: status 1 at line 6: bytes=$(stat -c %s "clips/$1")
clip_02.mp4 size:
report finished
exit status: 0
The first ERR: line comes from inside the command substitution, where stat failed. The second comes from the assignment on the same line, which takes the status of the substitution. Both are inside the function, and both now reach the trap. The script still finishes with status 0, because nothing here tells it to stop: the trap reports, it does not exit. If you want the run to fail, add set -e as well, or put exit 1 in the trap.
When the trap does fire without set -E
The trap is not blind to functions in every case. If the function's own return status is non-zero, the trap fires at the place where the function was called:
#!/usr/bin/env bash
trap 'echo "ERR: status $? at line $LINENO: $BASH_COMMAND" >&2' ERR
check_clip() {
echo "checking $1"
test -s "clips/$1"
}
check_clip clip_01.mp4
check_clip clip_02.mp4
echo "checks finished"
$ bash check.sh; echo "exit status: $?"
checking clip_01.mp4
checking clip_02.mp4
ERR: status 1 at line 10: test -s "clips/$1"
checks finished
exit status: 0
check_clip ends with test -s, so a missing clip makes the function return 1, and the trap runs. Look closely at the line it prints, though. Line 10 is the call, check_clip clip_02.mp4. The command text is the test -s line, the last command inside the function, on line 6. The message joins a line number from one place to a command from another. If you only read the number, you go to the call; if you only read the command, you go to the function body. Neither alone tells you which call failed when the same function runs many times.
Subshells behave the same way. A failure in a ( ... ) group is only seen when it decides the group's own status:
#!/usr/bin/env bash
trap 'echo "ERR: status $? at line $LINENO: $BASH_COMMAND" >&2' ERR
( cd clips && cp clip_02.mp4 ../backup/; echo "copied in subshell" )
echo "after subshell"
$ bash sub.sh; echo "exit status: $?"
cp: cannot stat 'clip_02.mp4': No such file or directory
copied in subshell
after subshell
exit status: 0
The cp failed, the group ended with a successful echo, and the trap stayed silent. With set -E:
#!/usr/bin/env bash
set -E
trap 'echo "ERR: status $? at line $LINENO: $BASH_COMMAND" >&2' ERR
( cd clips && cp clip_02.mp4 ../backup/; echo "copied in subshell" )
echo "after subshell"
$ bash sub_E.sh; echo "exit status: $?"
cp: cannot stat 'clip_02.mp4': No such file or directory
ERR: status 1 at line 5: cp clip_02.mp4 ../backup/
copied in subshell
after subshell
exit status: 0
The trap now fires inside the subshell, on line 5, and the subshell carries on to its echo, since nothing in this script asks it to stop.
Where set -eE still prints nothing
Adding -E does not make the trap fire everywhere. Calling the same upload function as the test of an if switches both mechanisms off for the whole call:
#!/usr/bin/env bash
set -eE
trap 'echo "ERR: status $? at line $LINENO: $BASH_COMMAND" >&2' ERR
upload() {
cp "clips/$1" backup/
echo "uploaded $1"
}
mkdir -p backup
if upload clip_02.mp4; then
echo "marked clip_02.mp4 done"
fi
$ bash upload_if.sh; echo "exit status: $?"
cp: cannot stat 'clips/clip_02.mp4': No such file or directory
uploaded clip_02.mp4
marked clip_02.mp4 done
exit status: 0
The cp failed, the trap did not run, set -e did not stop the function, and the script marked the clip as done with exit status 0. For -e, the manual lists the exemption directly: the shell does not exit when the failing command is "part of the test in an if statement", and the same page extends that to everything inside a function called from such a place. In this run the ERR trap went quiet in exactly the same spot. A function called on the left of && or ||, or in a while or until condition, deserves the same suspicion, because -e is switched off there as well.
No option fixes this one. The repair is an explicit || return on the line that can fail, which I covered in an earlier post on local and command substitution. What this run adds is only the trap side of it: -E widens where the ERR trap can reach, but it cannot reach into a place where -e itself is switched off.
A short audit for existing scripts
-
Find every ERR trap.
grep -n 'trap .* ERR' *.shlists them. For each hit, look forset -Eorset -o errtraceabove the first function call. Without one, the trap watches top-level commands and nothing else. -
Write the two options together.
set -eEon one line keeps them from drifting apart in a later edit. -
Read both halves of a trap message. Without
-E, a function returning non-zero gives you the call's line number next to a command from inside the body. With-Eyou also get the inner line. -
Treat
if,&&,||and loop conditions as trap-free zones. A function called there needs|| returnon the commands whose failure matters. -
Break it deliberately once. Delete a clip the job expects, run it, and look for the
ERR:line. No line means the trap is not watching the code that failed.
Limits of these runs
- One Bash. All of the output above comes from GNU Bash 5.1.16 on Linux. The quoted text is from the current online manual. The runs agreed with it, but no other release was tried.
- One shell. Whether dash, zsh or ksh pass an ERR trap into functions was not checked.
- No pipefail, no inherit_errexit. Neither option was on, and no pipeline was tested.
-
ERR only. The same manual page says
-Tdoes this job for the DEBUG and RETURN traps. That, and the EXIT trap, were left alone. - Toy files. The clip is a one-byte file and the missing clip was not created at all. The scripts copy into a local folder and print; nothing is uploaded or removed.
Sources. The quotations come from The Set Builtin in the GNU Bash Reference Manual. Every output block is the unedited result of running the script shown just before it.
I build an Android app, Background Camera RemoteStream on Google Play, that keeps an old phone recording with the screen off, which is how a backup job like this one ends up with a folder to copy. None of the scripts here need it.
An ERR trap set at the top of a Bash script does not run inside functions, command substitutions or subshells unless set -E is on, and even with it on, the trap stays silent everywhere set -e is switched off. A trap that printed nothing is not evidence that nothing failed.
Top comments (0)