As hinted at in this question, the answer is setting up a
trap on the
ERR signal - basically
set -e corresponds to
trap 'exit' ERR. So to e.g. log errors but continue execution, use
trap 'logger -t myscriptname "Command $BASH_COMMAND exited with code $?"' ERR
In bash, the variable
$BASH_COMMAND contains the offending command.
$? contains the exit code of the last command executed - note that if you want to perform multiple actions, e.g.
logger ...; echo $? the
$? will contain
logger's exit code, not
$BASH_COMMAND's one, so you may have to store it in a variable first, e.g.
trap 'EXITCODE=$?; logger -t "Command $BASH_COMMAND exited with code $EXITCODE"; echo "$BASH_COMMAND at line $LINENO exited with code $EXITCODE"' ERR
Of course you might be better off declaring a function and passing it the
$BASH_COMMAND instead of such a long line!