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?
For example:
From the reference for set:
-e
Exit immediately if a simple command (see section 3.2.1 Simple Commands) exits with a non-zero status, unless the command that fails is part of an until or while loop, part of an if statement, part of a && or || list, or if the command's return status is being inverted using !. A trap on ERR, if set, is executed before the shell exits.
(Emphasis mine).