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_EVENTParameters & Attributes
| Name | Type | Description |
|---|---|---|
| ACTION | operand | handler code or function name |
| SIGNAL_OR_EVENT | operand | signal 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; :)