Linux Command Reference ยท Shell Builtins
Linux trap command
Reference for the Bash trap builtin: run cleanup code on EXIT, handle signals, use ERR, DEBUG and RETURN traps, and list or reset handlers.
trap registers a command that the shell runs when a signal arrives or when a special shell event happens. The most common use is cleanup: delete a temporary file, release a lock or restore the terminal, no matter how the script ends. A trap on the pseudo-signal EXIT runs when the shell exits for any reason, including a normal end, an exit call and a fatal signal such as SIGINT or SIGTERM.
Besides real signals, Bash offers EXIT, ERR, DEBUG and RETURN, which respond to shell events rather than the kernel.
trap 'rm -f "$tmp"' EXIT trap 'echo "interrupted"; exit 130' INT TERM
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
trap [-lp] [[ACTION] SIGNAL ...] trap - SIGNAL ... trap '' SIGNAL ...
| Name | Description |
|---|---|
ACTION |
Shell code to run, as a single string (use single quotes so variables are expanded when the trap fires). |
SIGNAL |
A name (INT, SIGINT), a number, or EXIT (also 0), ERR, DEBUG, RETURN. |
- as action |
Reset the signal to its default behavior. |
'' as action |
Ignore the signal. |
-l |
List signal names and numbers. |
-p |
Print the traps currently set, in a form that can be reused as input. |
Returns: 0, or non-zero if a signal name is invalid. SIGKILL and SIGSTOP cannot be trapped.
Edge cases: after a trap handler for a fatal signal returns, the script continues unless the handler calls exit. A script killed by SIGINT should usually exit with 130 (128 plus the signal number) so a parent shell knows it was interrupted.
Usage notes
- Quote with single quotes.
trap "echo $x" EXITexpands$xwhen the trap is set;trap 'echo $x' EXITexpands it when it runs. Choose on purpose. - EXIT runs once. It fires when the shell finishes, even after
exitor an error underset -e. It does not run if the shell is killed bySIGKILL. - Subshells do not inherit traps. Command substitutions and
( ... )reset caught signals to default. Enableset -E(errtrace) so functions and subshells inherit theERRtrap too. ERRis conditional. It does not fire for a failure insideif,while,&&,||or!, the same places whereset -eis ignored.DEBUGruns before every simple command. It is useful for step tracing and profiling.RETURNfires when a function or sourced file finishes.- One handler per signal. Setting a trap again replaces the old one. To extend it, read it first with
trap -por keep a singlecleanupfunction. - Handlers waiting for a foreground command. Bash runs a trap for a signal only after the current foreground command finishes. A trap on
INTduring a longsleep 100waits for it to end unless the signal also kills the child. - Exit status inside a handler.
$?at the start of the handler is the status of the command before the trap; save it if you need it later.
Related
kill,wait,exit,set,mktemp- Lessons: Traps and signals, Kill and signals, Exit codes and errors