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:
#!/bin/bash
set -e
mkdir /tmp/foo
# ... do stuff ...
rm -r /tmp/foo
How can I ensure that /tmp/foo
is removed, even if one of the commands in ... do stuff ...
fails?