commands

trap — register a shell handler

Bash trap registers code for signals or events such as EXIT. It cannot catch SIGKILL; cleanup must be scoped and preserve failure status. Example assumes a fresh shell lab fixture with new output names.

Formal Syntax & Structure

linux syntaxlinux
trap ACTION SIGNAL_OR_EVENT

Parameters & Attributes

NameTypeDescription
ACTIONoperandhandler code or function name
SIGNAL_OR_EVENToperandsignal or event name

Return Value

exit status and output

Bash trap registers code for signals or events such as EXIT. It cannot catch SIGKILL; cleanup must be scoped and preserve failure status.

Working Example

Example Snippetlinux
(trap 'printf "done\\n"' EXIT; :)
Linux Reference