Skip to content
Tutorials Center
My learning

Topics

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.

Terminal
trap 'rm -f "$tmp"' EXIT
trap 'echo "interrupted"; exit 130' INT TERM

Try it

Bash Cleanup on exit and on errors

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.

Bash Catching a signal

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

Terminal
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" EXIT expands $x when the trap is set; trap 'echo $x' EXIT expands it when it runs. Choose on purpose.
  • EXIT runs once. It fires when the shell finishes, even after exit or an error under set -e. It does not run if the shell is killed by SIGKILL.
  • Subshells do not inherit traps. Command substitutions and ( ... ) reset caught signals to default. Enable set -E (errtrace) so functions and subshells inherit the ERR trap too.
  • ERR is conditional. It does not fire for a failure inside if, while, &&, || or !, the same places where set -e is ignored.
  • DEBUG runs before every simple command. It is useful for step tracing and profiling. RETURN fires 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 -p or keep a single cleanup function.
  • Handlers waiting for a foreground command. Bash runs a trap for a signal only after the current foreground command finishes. A trap on INT during a long sleep 100 waits 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.