Use this when scripts must clean up on exit or signals.
Trap
trap runs commands when a script receives a signal or exits, such as trap 'rm -f "$tmp"' EXIT, so temporary files are cleaned up and locks released even on failure.
1. trap runs handlers on EXIT, ERR, INT, TERM.
2. Clean temp files and locks.
3. Report line numbers on error.
4. Restore state.
5. Use mktemp for temp files.
signal/exit → trap handler → cleanup
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 Verification path Verify boundary
The negative fixture should deliberately exercise risks such as unquoted expansion, globbing, pipeline status loss, subshell scope, set -e misconceptions, signal races, temporary-file leakage and destructive path ambiguity, choosing the cases most relevant to signal/EXIT trap registration, ordering, cleanup idempotency, original exit status preservation and avoiding unsafe work in asynchronous signal contexts.
Step 2 Common mistakes Narrow the fault
- Cleaning up only on success.
- Traps that hide the original exit code.
Evidence · 1 on this Topic
Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.
- trap 'cleanup' EXIT runs on any exit.
- Trap INT and TERM for signals.
- Remove temp files and locks.
trap 'cleanup' EXIT— always cleanuptrap 'echo "error at line $LINENO" >&2' ERR— error contexttrap -p— list traps
ReferenceWhy it matters1 section · depth on demand
What it is
trap runs a command when the script receives a signal or exits, typically to clean up temp files or release locks.
ReferenceImportant terms & boundaries2 sections · depth on demand
In depth
trap runs a command when the script receives a signal or exits. It is how Bash scripts clean up — removing temporary files, releasing locks, reporting failures — no matter how the script ends.
set -euo pipefail
tmp=$(mktemp -d)
cleanup() { rm -rf "$tmp"; }
trap cleanup EXIT
trap 'echo "error on line $LINENO: $BASH_COMMAND" >&2' ERR
trap 'echo "interrupted" >&2; exit 130' INT TERM
curl -sf "$URL" -o "$tmp/data.json"
| Signal | When |
|---|---|
| EXIT | Script ends for any reason |
| ERR | A command fails (with set -e semantics) |
| INT | Ctrl+C |
| TERM | kill / service stop |
SIGKILL (kill -9) cannot be trapped, so critical cleanup should also tolerate leftovers from a killed run.
Example
tmp=$(mktemp)
trap 'rm -f "$tmp"' EXIT
trap 'echo interrupted; exit 130' INT TERM
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
Authoritative/upstream documentation reviewed for bounded claims in Bash Automation > Trap. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://www.gnu.org/software/bash/manual/bash.html · confidence 94%Deep reference
Healthy vs suspicious evidence
Output interpretation
For signal/EXIT trap registration, ordering, cleanup idempotency, original exit status preservation and avoiding unsafe work in asynchronous signal contexts, prefer typed fields and stable IDs over human presentation text.