trap - Trap signals
Synopsis
trap [ action condition ... ]
trap -p [ condition ... ]
trap -l
Description
The trap utility sets the action
for qsh to take when a condition arises.
qsh expands action once when running
trap and again when condition arises.
When the -p option is specified,
trap displays the current action for the specified
conditions.
When the -l option is specified,
trap displays a list of all of the signal names and their
corresponding numbers.
When no arguments are specified, trap displays a list
of the currently defined traps.
Options
-
-l
- Display a list of all of the signal names and their corresponding numbers.
- -p
- Display each trap in a re-enterable format.
Operands
For action, you can specify:
- Null to ignore condition when it arises
- Minus (-) to reset condition to its
original value.
- A command to be run each time condition arises.
For condition, you can specify:
- Name or number of a signal. You can use trap
-l to display a list of valid signals. For portability,
you should always specify the signal name.
- 0 or EXIT.
qsh runs action when the shell exits.
- ERR. qsh runs action
when a command has a non-zero exit status.
- DEBUG. qsh runs
action after each simple command.
If more than one condition arises at the same time,
qsh runs the traps in this order:
- DEBUG, if it is specified, then
- ERR, if it is specified and applicable,
then
- Any other specified traps in signal number order, then
- EXIT.
Exit Status
- 0 when successful.
- >0 when an invalid condition is specified.
Related information
Examples
- Set a trap for the ERR condition:
trap `print Command failed' ERR
- Ignore the ERR condition:
trap "" ERR
- Reset the ERR condition to its original value:
trap - ERR
- Display the current action for the ERR condition:
trap -p ERR
- Display all of the currently defined traps:
trap