How to run a command before a Bash script exits?


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:

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?

11/4/2017 4:32:44 AM

Accepted Answer

Here's an example of using trap:

#!/bin/bash -e

function cleanup {
  echo "Removing /tmp/foo"
  rm  -r /tmp/foo

trap cleanup EXIT
mkdir /tmp/foo
asdffdsa #Fails


dbrown@luxury:~ $ sh traptest
t: line 9: asdffdsa: command not found
Removing /tmp/foo
dbrown@luxury:~ $

Notice that even though the asdffdsa line failed, the cleanup still was executed.

8/14/2019 7:50:44 PM

From the bash manpage (concerning builtins):

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow