builtin

trap

Register, inspect or reset fixed actions for signals or Bash pseudo-events. An empty action ignores a signal, a dash restores its default; KILL and STOP cannot be caught. Quote the action for evaluation at event time.

Formal Syntax & Structure

bash syntaxbash
trap [-lp] [[action] event ...]

Parameters & Attributes

NameTypeDescription
actionshell codeTrusted action or reset marker.
eventsignal or pseudo-eventSignal name/number or supported pseudo-event.

Return Value

exit status

Register, inspect or reset fixed actions for signals or Bash pseudo-events. An empty action ignores a signal, a dash restores its default; KILL and STOP cannot be caught. Quote the action for evaluation at event time.

Working Example

Example Snippetbash
bash -c '    on_int() { printf "INT\n"; exit 130; }    trap on_int INT    kill -INT "$$"    printf "unreachable\n"'printf 'child=%s\n' "$?"
Bash Reference