If a Bash script has set -e, and a command in the script returns an error, how can I do some cleanup before the script exits?
set -e
For example:
From the bash manpage (concerning builtins):
bash
trap [-lp] [[arg] sigspec ...] The command arg is to be read and executed when the shell receives signal(s) sigspec.
So, as indicated in Anon.'s answer, call trap early in the script to set up the handler you desire on ERR.
trap