intercept abnormal conditions and interrupts 

KornShell Built-in


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


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 window is closed externally or when a shutdown or logoff event occurs. 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 CLOSE, trap invokes the handler when the shell's console window is closed externally. The close window button, the system menu, the task manager, and the kill command all close the console window externally.

With an event of LOGOFF, trap invokes the handler after Windows has been issued a shutdown or logoff command. The handler in this case can only run commands built into the shell.


Any traps executed after a CLOSE or LOGOFF event have less than five seconds to complete or the operating system prompts the user that the application has not responded to the close message.

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.

Because the MKS KornShell is not a NuTCRACKER Platform application, trap utility can only handle system generated events such as CTRL-C. It cannot receive kill signals from another non-NuTCRACKER Platform application. Using the command kill -1 on the shell itself (sh.exe) results in a call to TerminateProcess() not a trappable signal.


When the shell is in Korn mode (as set by -K or -o korn), any traps set within a function only apply when that function is being executed. When the shell is not in Korn mode, traps set within a function apply from that point on in the script (until another trap command replaces it).


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



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.


Possible exit status values are:


Successful completion.


Failure due to any of the following:

— invalid signal name
— invalid signal number

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.


POSIX.2. x/OPEN Portability Guide 4.0. Windows 7. Windows Server 2008 R2. Windows 8. Windows Server 2012. Windows 10. Windows Server 2016.

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.


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


PTC MKS Toolkit for Power Users
PTC MKS Toolkit for System Administrators
PTC MKS Toolkit for Developers
PTC MKS Toolkit for Interoperability
PTC MKS Toolkit for Professional Developers
PTC MKS Toolkit for Professional Developers 64-Bit Edition
PTC MKS Toolkit for Enterprise Developers
PTC MKS Toolkit for Enterprise Developers 64-Bit Edition
PTC Integrity



PTC MKS Toolkit 10.1 Documentation Build 15.