trap
TLDR
List available signal names
SYNOPSIS
trap [command] [signal...]
trap -l
trap -p [signal...]
DESCRIPTION
trap is a shell builtin that executes commands when the shell receives signals. It's commonly used in scripts for cleanup operations (removing temp files), handling interrupts gracefully, and ignoring signals that would otherwise terminate the script.
Common signals include SIGINT (Ctrl+C), SIGTERM (termination request), SIGHUP (hangup), EXIT (script exit), and ERR (command error). The EXIT pseudo-signal executes on normal script exit.
PARAMETERS
-l
List signal names and numbers-p [signal]
Print trap commands for specified signalscommand
Command to execute when signal is received- signal
Reset signal to default behavior'' signal
Ignore the signal
CAVEATS
Trap is a shell builtin, not an external command. Behavior varies slightly between shells (bash, dash, zsh). Some signals (SIGKILL, SIGSTOP) cannot be trapped. Traps are inherited by subshells but not by external commands.
HISTORY
Originated in the Bourne shell in Unix V7 (1979). The concept of trapping signals comes from the Unix signal handling mechanism. Bash and other modern shells have extended the original functionality with pseudo-signals like ERR and DEBUG.


