Systems Admin Info

Exit Traps

This is a really cool way to help clean up temp files etc, when a script end Bash Exit Traps

The function fiinsh is called upon exit, so you can put your clean up stuff there, instead of the end of the script (which may not get run if the script crashes)

#!/bin/bash
function finish {
  # Your cleanup code here
}
trap finish EXIT

Or with a full example:

#!/bin/bash
scratch=$(mktemp -d -t tmp.XXXXXXXXXX)
function finish {
  rm -rf "$scratch"
}
trap finish EXIT