trap Command: Tutorial & Examples

The trap command is an internal command of the shell that allows you to control the actions to be taken when a signal is received by a process. The trap command is used to specify commands that will be executed when a process receives an interrupt signal. There are many interrupt signals that a process can receive, such as SIGINT ( Signal Interrupt), SIGQUIT (Signal Quit), SIGSTOP (Signal Stop), and many others. When a process receives an interrupt signal, it can respond in a variety of ways. By default, some signals result in the process terminating. However, with the trap command, you can configure specific commands to be executed when a process receives an interrupt signal.

How does the trap command work?

The trap command uses the following syntax:

trap command signal

Where,

  • command is the command you want to be executed when a process receives an interrupt signal.
  • signal is the name or number of the signal you want to catch.

You can also use the trap command without any arguments to list the commands that are currently being trapped.

What is the trap command used for?

The trap command is used to handle interrupt signals in a script or a process. For example, you might want to clean up temporary files when a script is terminated or ensure that a script finishes a task before it is interrupted.

Here is an example of how you can use the trap command:

trap "echo 'Script interrupted... Cleaning up'; rm -f /tmp/tempfile" INT

In this example, if you interrupt the script by pressing Ctrl+C (which sends the INT signal), the script will print "Script interrupted... Cleaning up" and then delete the /tmp/tempfile.

Why is the trap command important?

The trap command is essential for writing robust shell scripts. By trapping interrupt signals, you can ensure your scripts clean up after themselves and finish critical tasks even when they're interrupted. This can be especially important in production environments, where scripts might be responsible for important tasks like updating databases or processing large data sets.

Common trap command parameters

There are several common parameters you can use with the trap command:

  • -l: Prints a list of signal names and their corresponding numbers.
  • -p: Prints a list of commands that are currently being trapped.

Potential problems and pitfalls

While the trap command is very useful, there are some potential problems and pitfalls to be aware of:

  • Not all signals can be trapped. For example, the SIGKILL and SIGSTOP signals cannot be caught, blocked, or ignored.
  • If you trap a signal and then forget to untrap it, the trap will remain in effect until the shell is exited. This could interfere with subsequent scripts or commands.

Examples: How to use the trap command

Here are some examples of how you can use the trap command:

  1. Trapping the INT signal:

    trap "echo 'Interrupt signal received, cleaning up...'" INT
    

    When you press Ctrl+C, the script will print "Interrupt signal received, cleaning up..." instead of immediately terminating.

  2. Trapping multiple signals:

    trap "echo 'Interrupt or terminate signal received, cleaning up...'" INT TERM
    

    In this example, the script will print "Interrupt or terminate signal received, cleaning up..." if it receives either an INT or TERM signal.

  3. Removing a trap:

    trap - INT
    

    This command removes the trap for the INT signal. After this command is executed, the INT signal will be handled by the shell's default handler.

Remember, the trap command is a powerful tool for handling interrupt signals, but it should be used with care. Always test your scripts thoroughly to ensure they handle signals as expected.

Except where otherwise noted, content on this site is licensed under a CC BY-SA 4.0 license CC BY SA