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:

  1. DEBUG, if it is specified, then
  2. ERR, if it is specified and applicable, then
  3. Any other specified traps in signal number order, then
  4. EXIT.

Exit status

  • 0 when successful.
  • >0 when an invalid condition is specified.

Examples

  1. Set a trap for the ERR condition:
    
    trap `print Command failed' ERR
    
  2. Ignore the ERR condition:
    
    trap "" ERR
    
  3. Reset the ERR condition to its original value:
    
    trap - ERR
    
  4. Display the current action for the ERR condition:
    
    trap -p ERR
    
  5. Display all of the currently defined traps:
    
    trap