trap

intercept abnormal conditions and interrupts

KornShell Built-in


SYNOPSIS

trap ['handler'] [event ...]


DESCRIPTION

trap intercepts certain kinds of exception conditions. Any signal may be intercepted by specifying an event corresponding to the signal number or by specifying the signal number itself.

With an event of ERR, trap invokes the handler upon any command having a non-zero exit status. The exception to this is conditions in if, while, and until statements. This trap is not inherited within a function.

With a trap number of 0 or EXIT, trap invokes the handler during exit from the shell. Within a function, it is invoked during exit from the function. The EXIT trap is not called when the shell's console is closed externally. According to both POSIX.2 and XPG, handler for an EXIT happens in the same environment as the last command run before the EXIT. For traditional KornShell behavior, set the shell variable korn.

With an event of LOGOFF, trap invokes the handler. The handler in this case can only run commands built into the shell.

Any other event corresponds to a signal number or signal name. On Win32 platforms, the valid signal numbers are 2 (SIGINT) and 14 (SIGALRM). SIGALRM is sent when the TMOUT variable expires. You can use the names INT, STOP, ALRM, and TERM instead of the signal numbers. If a signal is being ignored when you enter the shell, the shell continues to ignore it without regard to any traps.

The handler argument is a command list. It is usually more than one word, and so you must quote it to appear as a single argument. It is scanned when the trap function is initially invoked. When the trap condition is raised, the shell scans the command list again and executes the commands. A missing argument or an argument of - (dash) resets the default trap condition. A null argument ('') causes the trap condition to be ignored.

If there are no arguments at all, trap prints a list of all the traps and their commands.


EXAMPLES

On error or exit, this example deletes a temporary file created during command execution.
trap 'rm -f /tmp/xyz$$; exit' ERR EXIT

Upon an interrupt signal, this example asks whether to abort, and exits if the answer is y.

trap 'read REPLY?"ABORT??"
  case $REPLY in
  y)     exit 1;;
  esac'  2

This example saves your shell history file (specified by the value you give the HISTFILE environment variable) before timing you out, so you can restore it when you log in again.

trap 'cp $HISTFILE $HOME/old_hist.bak; exit' ALRM

ENVIRONMENT VARIABLES

TMOUT

contains the number of seconds before user input times out. If user input is not received within this length of time, the shell terminates. A previous example shows you how to trap a timeout (TMOUT) event.


DIAGNOSTICS

Possible exit status values are:
0

Successful completion.

1

Failure due to any of the following:

— invalid signal name
— invalid signal number
2

Failure due to an invalid command line option.

"name" Not a valid trap name

You specified an unrecognized trap name. The usual cause of this error is a typing mistake on the command line.


PORTABILITY

POSIX.2. x/OPEN Portability Guide 4.0.

A built-in function of the Bourne Shell and KornShell on UNIX systems.

The ability to handle EXIT traps in the KornShell style is an extension to the POSIX.2 and XPG standards.


NOTE

This is a special built-in command of the MKS KornShell.


SEE ALSO

Commands:
sh


Updated MKS Toolkit [3khat16.ico]HP3000 [3khat16.ico]3kMail [archive16.gif]