Dealing with temporaries in bash.rst

It is quite common that I want to create a temporary file or directory during the exectuion of a bash script. For this we may use mktemp and mktemp -d respectively.

The problem that arises is that you are not sure that your clean up code gets executed if the user aborts the script by e.g. hitting Ctrl+c. An elegant solution was given here

to illustrate the solution let's consider the following script:

TMPDIR=$(mktemp -d)
echo $TMPDIR
cleanup() {
    rm -r $TMPDIR
trap cleanup INT TERM
echo "A" | tee $TMPDIR/a.txt
sleep 3  # Try hitting Ctrl-C here (use Ctrl-Z to inspect files)
echo "B" | tee $TMPDIR/b.txt
echo "C" | tee $TMPDIR/b.txt # will never be reached

The above code removes the temporary directory created at exit, no matter if the script is allowed to finnish or aborted by the user invoking kill on the PID (defaults to SIGTERM) or hits Ctrl+C (sends the SIGINT signal).


Comments powered by Disqus