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 -ptrap -- 'echo ...' SIGINT
trap - INTremove a trap
trap -levery signal's name and number