The trap command

Trap signals and other events. 7 examples from the lessons of the IVski Linux Playground — try them in the terminal next to them.

Options in the examples

-l
print a list of signal names and their corresponding numbers
-p
display the trap commands associated with each SIGNAL_SPEC

Examples

trap 'rm -f "$TMP"' EXIT
trap 'rm -f "$TMP"; echo "cleaned up $TMP"' EXIT
trap 'echo "Something failed — stopping." >&2' ERR
trap 'echo "Interrupted — cleaning up"; exit 130' INT TERM
trap -p

trap -- 'echo ...' SIGINT

trap - INT

remove a trap

trap -l

every signal's name and number

Lessons that use trap