created on | January 18, 2022 |
trap signals that are sent to the shell script:
where is the signal (or signals) to trap and is the code to execute when a signal is trapped. can span multiple lines:
can be any signal that can be sent to a process. For a complete list of all signals, use . Two special signals are available:
SIGKILL and SIGSTOP can’t be trapped or ignored by any process.
the default action for a signal is restored with
trapping signals and restoring defaults of signals can be used to define the scope of traps.
the scope of a trap of a signal is from the position of the trap definition to the end of the script file or the restoring of the defaults for the signal.
in the following example, is executed, whenever in code block B a value not equal to 0 (zero) is assigned to , but not in code block A or code block C:
if the script is running in the foreground in the current shell, signals can be send to the script with a key combination.
signal | key combination |
---|---|
INT | CTRL-C |
QUIT | CTRL-\ |
TSTP | CTRL-Z |
STOP | CTRL-S |
CONT | CTRL-Q |
for clean up on exit of a script, the code executed by trap must include . otherwise the script does not exit.