EXIT trap
A trap on the special name EXIT, whose handler runs whenever the shell exits: at the end, on exit, or when set -e stops it.
In more detail
It is the one place to put cleanup such as deleting a temporary directory. Set it straight after creating what it cleans up. The handler should leave the script's exit status alone so callers still see the real result.
Related terms
- trap builtin — The builtin that runs a command or function when the shell receives a signal or exits, used for cleanup.
- mktemp — The command that safely creates a new, uniquely named temporary file, or with -d a directory, and prints its path.
- idempotency — The property of an operation that gives the same result whether you run it once or many times, because a repeat run finds nothing left to do.
Taught in
- trap, Cleanup And Temporary Files — Bash Scripting & Command Line Automation
- Processes, Jobs And Signals — Bash Scripting & Command Line Automation