The trap builtin command
trap [-lp] [[ARGUMENT] SIGNAL]
trap command is used to "trap" signals and other events. In this context, "trapping" means to install handler code.
The shell code
ARGUMENT is to be read and executed whenever the shell receives a signal or another event
SIGNAL. The given
SIGNAL specification can be
- the name of a signal with the SIG prefix, e.g.
- the name of a signal without the SIG prefix, e.g.
- the number of a signal (see
trap -l), e.g.
- the name or number of a special event (see table below), e.g.
Without any options or operands,
trap prints a list of installed traps in a reusable format (equivalent to the
ARGUMENTis absent or
-(dash), the signal/event handler is reset to its original value
ARGUMENTis the null string, the signal/event is ignored
|0||executed on shell exit|
|executed before every simple command|
|executed when a shell function or a sourced code finishes executing|
|executed each time a command's failure would cause the shell to exit when the ''-e'' option (''errexit'') is enabled|
|print a list of signal names and their corresponding numbers|
|display the trap commands associated with each signal specification in a reusable format|
|!=0||invalid signal specification|
List installed traps
Ignore terminal interrupt (Ctrl-C, SIGINT)
trap '' INT
trapis specified by POSIX(R) without the
- in POSIX(R), beside signals, only
EXIT(0) is valid as an event
- the set command for the