trap cleans up temp files even when the script dies
A script that makes a temp directory and deletes it at the end leaves the directory behind every time it exits early. trap runs the cleanup on any exit.
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXITEXIT fires on success, on error under set -e, and on Ctrl-C. The file system stays clean either way.
shell