trap builtin

The builtin that runs a command or function when the shell receives a signal or exits, used for cleanup.

In more detail

trap cleanup EXIT runs cleanup however the script ends, and adding INT and TERM covers Control C and kill. trap with no arguments lists the handlers and trap - EXIT removes one. SIGKILL cannot be trapped, so no handler runs when a process is killed that way.

Example

trap cleanup EXIT INT TERM

Related terms

Taught in

References