- Published on
Short engineering note
Today I Learnt: Trap signals and cleanup
- Authors

- Name
- Mehdi Akiki
Note · Engineering notes
Create the temporary file before registering the cleanup handler:
tmpfile=$(mktemp)
trap 'rm -f "$tmpfile"' EXIT
Use a function when cleanup needs more than one command:
cleanup() {
rm -f "$tmpfile"
}
trap cleanup EXIT INT TERM