Skip to content

trap Linux Command Guide

The trap command in Linux allows users to set and execute commands upon receiving signals. This feature enhances script functionality and error handling, making it a valuable tool for system administrators and developers. By using the trap command, you can define specific actions to take when signals are received, such as cleaning up temporary files or logging information before exiting a script. Additionally, the trap command provides a way to handle errors gracefully and improve the overall reliability of your scripts.

trap Syntax:

Terminal window
trap [action] [signals]

Options:

OptionDescription
-Not applicable

Parameters:

ParameterDescription
actionThe action to be taken when the specified signals are received
signalsOne or more signals to trap and execute the specified action for

trap Command Samples:

Trap a Signal and Execute a Command

Terminal window
trap "echo 'Signal received'" SIGINT

This command traps the SIGINT signal (usually generated by pressing Ctrl+C) and executes the specified command when the signal is received.

Execute Cleanup Commands when Exiting

Terminal window
trap "rm -f /tmp/tempfile" EXIT

A trap command that ensures a temporary file is removed when the script or shell session exits.

Ignore a Specific Signal

Terminal window
trap "" SIGHUP

Using an empty string as the action in the trap command ignores the SIGHUP signal.

Trap and Handle Errors Gracefully

Terminal window
trap "echo 'Error occurred'; exit 1" ERR

Setting up a trap to notify about errors and exit the script with an error status if an error occurs.

Execute a Command on Termination

Terminal window
trap "echo 'Terminating...'; cleanup_function" TERM

The script will execute the cleanup_function and display a termination message when receiving the TERM signal (for example, when running the “kill” command).

Trap a Combination of Signals

Terminal window
trap "echo 'Received SIGINT or SIGTERM'" SIGINT SIGTERM

This command traps both the SIGINT and SIGTERM signals and executes the specified action when either signal is received.

Reset a Trap

Terminal window
trap - SIGINT

Removing the trap on the SIGINT signal, which will revert the default behavior of the shell for that signal.

trap FAQ:

How do I use trap in Linux?

To use the trap command in Linux, execute the following command:

Terminal window
trap --option <value>

How can I trap a specific signal in Linux?

To trap a specific signal in Linux using the trap command, you can do the following:

Terminal window
trap "echo 'Signal trapped'" SIGINT

How do I ignore a signal using trap in Linux?

To ignore a specific signal using trap in Linux, you can use the following syntax:

Terminal window
trap "" SIGTERM

How do I display the list of traps set in Linux?

To display the list of traps that are currently set in Linux, you can use the command:

Terminal window
trap -p

How can I reset a trap in Linux?

To reset a trap in Linux and remove any previously set trap, you can use the following command syntax:

Terminal window
trap - INT

How do I execute a command when a script exits in Linux?

To execute a command when a script exits in Linux, you can use the trap command with the EXIT signal:

Terminal window
trap "echo 'Script exiting'" EXIT

Applications of the trap command

  • Running specific cleanup commands before exiting a script
  • Capturing and handling signals sent to a script or process
  • Managing the behavior of a script in response to certain events or errors
  • Gracefully terminating a script or process and performing cleanup actions