Name
trap
Synopsis
trap [option] [commands] [signals]Execute commands if any of
signals is received. Each
signal can be a signal name or number.
Common signals include 0, 1, 2, and 15. Multiple commands should be
quoted as a group and separated by semicolons internally. If
commands is the null string (e.g., trap “”
signals), then
signals is ignored by the shell. If
commands is omitted entirely, reset
processing of specified signals to the default action. If both
commands and
signals are omitted, list current trap
assignments. See the examples at the end of this entry and under
exec.
Options
- -l
List signal names and numbers.
- -p
Used with no
commandsto print the trap commands associated with eachsignal, or all signals if none is specified.
Signals
Signals are listed along with what triggers them.
- 0
Exit from shell (usually when shell script finishes).
- 1
Hang up (usually logout).
- 2
Interrupt (usually through Ctrl-C).
- 3
Quit.
- 4
Illegal instruction.
- 5
Trace trap.
- 6
Abort.
- 7
Unused.
- 8
Floating-point exception.
- 9
Termination.
- 10
User-defined.
- 11
Reference to invalid memory.
- 12
User-defined.
- 13
Write to a pipe without a process to read it.
- 14
Alarm timeout.
- 15
Software termination (usually via kill).
- 16
Unused.
- 17
Termination of child process.
- 18
Continue (if stopped).
- 19
Stop process.
- 20
Process suspended (usually through Ctrl-Z).
- 21
Background process has tty input.
- 22
Background process has tty output.
- 23-28
Unused.
- 29
I/O possible on a channel.
Examples
trap "" 2Ignore signal 2 (interrupts)trap 2Obey interrupts again
Remove a $tmp file ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access