Is it possible to distribute the exit code to the caller in the event of a syntax error in a Bash script with an EXIT trap? For example, if I have:
#! /bin/bash
set -eu
trap "echo dying!!" EXIT
echo yeah
echo $UNBOUND_VARIABLE
echo boo
Then, by running it, you will get exit code 0, even if the script did not complete successfully:
$ bash test.sh
yeah
test.sh: line 8: UNBOUND_VARIABLE: unbound variable
dying!!
$ echo $?
0
But if I comment on the output trap, the script returns 1. Alternatively, if I replace the string with an unbound variable with a command that returns a non-zero value (for example /bin/false), this output value propagates as I would like it to.
source
share