Pcornel
Browse topics
Options
Recent
Favorites
Automation · Technical reference

Trap

Direct answer

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.

When to use this

Use this when scripts must clean up on exit or signals.

First 30 seconds

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.

Operational verification

signal/exit → trap handler → cleanup

Context & applicabilityCommon mistakes · platform/version boundaries
Platform / version applicability
GNU Bash shell automation
applies
Structured applicability is explicit Topic metadata; it does not infer compatibility beyond the stored rows.
At a glance

Operational reference

Fast context before you go deeper.

PlatformGNU Bash shell automation · Automation implementations are tool and platform specific. Use the adjacent Domain for the technology being automated and this Domain for automation behavior.
Scope / prerequisiteUse this when scripts must clean up on exit or signals.
What next?3 suggested next steps
Evidence1
On this pageJump to a specific section
Operational workflow

Verification & Troubleshooting

Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.

2 steps Observe → verify → isolate → change minimally → repeat the original test.
ObservedRecord what the original test actually shows

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 references

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.

observation · 1
Key points
  • trap 'cleanup' EXIT runs on any exit.
  • Trap INT and TERM for signals.
  • Remove temp files and locks.
Key checks
  • trap 'cleanup' EXIT — always cleanup
  • trap 'echo "error at line $LINENO" >&2' ERR — error context
  • trap -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"
SignalWhen
EXITScript ends for any reason
ERRA command fails (with set -e semantics)
INTCtrl+C
TERMkill / service stop

SIGKILL (kill -9) cannot be trapped, so critical cleanup should also tolerate leftovers from a killed run.

Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Technical verification history

Verification history & basis

When and against what this Topic was checked. Later text edits do not count as a new check.

2026-09-14 · vendor docGNU Bash shell automation current

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%
Depth when you need it

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.

Confirmation

Confirm action

Continue?