Bash Trap Command

ShellShellBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In Bash scripting, the trap command allows you to catch and handle signals, interruptions, and user inputs in your script. By using trap, you can define specific actions to be taken when a particular signal is received, allowing you to prevent unpredictable behavior and gracefully handle various scenarios.

Create a Bash Script

Create a new file ~/project/trap_example.sh. This will be our Bash script where we will implement the trap command.

cd ~/project
touch trap_example.sh

Declare the Trap Command

Inside the trap_example.sh file, declare the trap command to catch specific signals and define the actions to be taken when those signals are received.

#!/bin/bash
## trap_example.sh

trap "echo Booh!" SIGINT SIGTERM
echo "The script will run until you hit Ctrl+Z."
echo "Hit Ctrl+C to display a message!"

while true; do
  sleep 60
done

In this example, we are catching the SIGINT (interrupt) and SIGTERM (termination) signals. When either of these signals is received, the script will execute the specified action, which, in this case, is to display the message "Booh!".

Run the Script

Make the script executable using the chmod +x command:

chmod +x trap_example.sh

Run the script:

./trap_example.sh

Test the Trap Command

While the script is running, try the following actions to test the trap command:

  1. Press Ctrl+C - The script should display the message "Booh!".
  2. Press Ctrl+Z - The script should continue running in the background.
  3. Run kill -l in the terminal - This command will display a list of all available signals. Take note of their numbers.

Refactor the Trap Action

Instead of using an inline message in the trap command, let's refactor it to use a function. Add a function definition to the script:

function booh {
  echo "Booh!"
}

Now, modify the trap command to use the booh function as the action:

trap booh SIGINT SIGTERM

Use Signal Numbers in the Trap Command

Instead of using signal names in the trap command, you can also use their corresponding numbers. We can use the earlier command kill -l to determine the numbers. Modify the trap command to use signal numbers:

trap booh 2 15

In this example, 2 corresponds to SIGINT and 15 corresponds to SIGTERM.

Perform Cleanup with Trap

One common usage of the trap command is to perform cleanup activities, such as deleting temporary files. Add a command to delete a folder and exit the script when a specific signal is received:

trap "rm -f folder; exit" 2

In this example, when the SIGINT signal is received (Ctrl+C), the script will delete the folder named folder and then exit gracefully.

Summary

In this lab, you learned how to use the trap command in Bash scripting. By catching specific signals and defining the actions to be taken, you can control the behavior of your script and handle user inputs and interruptions effectively. The trap command allows you to gracefully handle unexpected scenarios and perform cleanup activities when necessary.

Other Shell Tutorials you may like