A signal is an asynchronous event that can happen in a program. The
operating system defines the possible kinds of signals, and gives each
kind a name and a number. For example, in Unix SIGINT
is the
signal a program gets when you type an interrupt character (often
C-c); SIGALRM
occurs when the alarm clock timer goes off
(which happens only if your program has requested an alarm).
Some signal handlers are installed and changed for the BASH debugger’s
normal use: SIGDEBUG
and SIGEXIT
. SIGDEBUG
is
used by the debugger to potentially stop your program before execution
of each statement occurs, and SIGEXIT
is used to catch your
program just before it is set to leave so you have the option of
restarting the program with the same options (and not leave the
debugger) or let the program quit.
Signal handlers that the debugged script might have installed are
saved and called before the corresponding debugger handler. Thus, the
debugged program should work roughly in the same fashion as when it is
not debugged. However there are some call-stack variables which
inevitably will differ. To try to hedge this a little so the behavior
is the same, the BASH debugger will modify arguments to the traps if it
finds one of the call-stack that change as a result of the debugger
being in place. In particular $LINENO
will get replaced with
${BASH_LINENO[0]}
; also ${BASH_LINENO[0]}
and
${BASH_SOURCE[0]}
get replaced with
${BASH_LINENO[1]}
and ${BASH_SOURCE[1]}
respectively.
The debugger also installs an interrupt handler SIGINT
so that
errant programs can be interrupted and you can find out where the
program was when you interrupted it.
Some signals, including SIGALRM
, are a normal part of the
functioning of your program. Others, such as SIGSEGV
, indicate
errors; these signals are fatal (they kill your program immediately) if the
program has not specified in advance some other way to handle the signal.
SIGINT
does not indicate an error in your program, but it is normally
fatal so it can carry out the purpose of the interrupt: to kill the program.
BASH has the ability to detect any occurrence of a signal in your program. You can tell BASH in advance what to do for each kind of signal.
Normally, BASH is set up to let the non-erroneous signals like
SIGALRM
be silently passed to your program
(so as not to interfere with their role in the program’s functioning)
but to stop your program immediately whenever an error signal happens.
You can change these settings with the handle
command.