Skip to content

.tip

· one of the small things

trap cleans up temp files even when the script dies

A script that creates 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"' EXIT

EXIT fires on success, on an error under set -e, and on Ctrl-C, so the file system stays clean whichever way it ends.

shell