O'Reilly logo

Linux and Unix Shell Programming by David Tansley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

26.3. trap

The trap command allows you to trap incoming signals to your scripts. The format of the trap command is:

trap name signal(s) 

The name is a list of instructions to be performed when the signal is caught. In reality a function should be called that deals specifically with trapped signals. The name should be surrounded by double quotes (" "). The signal(s) are the incoming signals.

When a signal is trapped a script will generally be in the middle of some processing. The most common form of action to take is:

1.
Clean up temporary files.
2.
Ignore the signals.
3.
Ask the user if the script is really to be terminated.

Here’s a table of the most common trap usages:

trap " " 2 3 Ignore the signals 2 and 3; user cannot terminate
trap ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required