Open In App

Shell Scripting – Bash Trap Command

Last Updated : 07 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The trap command in Bash executes specific code in response to the signal given to the program. It traps the signal received by the program from the OS or the user and then executes the specific code provided by the programmer. It acts like a trigger in response to a signal. Just like try-catch statements help in exception handling, trap command helps in signal handling.

Prerequisites:  How to Send a signal to Processes, Bash Scripting, Shell Function Library

Syntax and Working of Trap command

trap [-options] “piece of code” [signal name or value]

Example 1: SIGNIT Signal

Go to the Bash terminal and write the following command :

$ trap "echo Hello World" SIGINT

Now on pressing Ctrl+C, SIGINT(2) signal is sent to the terminal which is trapped by the trap command and the code echo hello world will be executed instead.

Ctrl+C sends SIGINT signal. We trap the signal and upon pressing Ctrl+C, the code is executed.

 

To revert back or remove the trap, write the command :

$ trap - SIGINT
Removing the trap of SIGINT signal

 

Example 2: STOP signal

Write the following command :

$ trap “echo This is a response to STOP signal” 20

Trapping SIGTSTP(20) signal

 

Signal value 20 corresponds to the stop signal SIGTSTP. Now on pressing Ctrl+Z, SIGTSTP(20) signal is sent to the terminal which is trapped by the trap command, and the code is executed instead. To revert back write trap – 20

Example 3: Printing piece of the code (SIGINT)

$ trap "echo response to SIGINT" SIGINT
$ trap "echo response to SIGTSTP" SIGTSTP
$ trap -p
Listing the args/commands associated with each trapped signal

 

If only the -p option is given, then it prints the piece of code we wrote associated with each signal.

$ trap -p SIGINT
Listing the command/piece of code associated with a particular SIGINT signal

 

If the -p option is given with a signal value or signal name, then the commands/piece of code associated with that signal is displayed.

Example 4: Listing all the signals

$ trap -l
Listing all signals that can be trapped

 

Lists all the signals that can be trapped.

Creating Shell Script

Let us understand the working of the trap command in detail by creating and executing some shell scripts.

Script 1: Hello World

The name of the script will be bashTrap.sh

$ touch bashTrap.sh

Make it an executable file.

$ chmod +x bashTrap.sh

Open the file and add the following script.

bashTrap.sh:

#!/bin/bash

trap "echo Hello World" SIGINT
while [[ true ]] ; do
    sleep 1
done

Run the script as a foreground process

$ ./bashTrap.sh
Ctrl+C doesn't terminate the process as the trap command traps the signal and executes the code

 

  • Whenever we press the keystroke Ctrl+C, it sends an Interrupt signal SIGINT (signal value 2) to the program
  •  Instead of the program getting terminated, the trap command traps the SIGINT signal and executes the code provided by us.
  • The infinite while loop is to make sure that the process keeps running and doesn’t exit too quickly before we send the signal to the process.
  • Finally, to get back to the shell prompt, press Ctrl+Z which will pause the process and then kill the process via the command kill %jobspec.

Script 2: Goodbye

Pseudo-signal EXIT(0) is received when the program exits from the shell. We can trap this signal and print a message when the shell process terminates.

bashTrap.sh:

#!/bin/bash

trap "echo Goodbye!" EXIT
sleep 1
When shell process exits, the exit signal is trapped and the command "echo goodbye" is executed

 

Script 3: Function

We can also trigger different function calls in response to different signals with the help of a trap command. Run the following script as a background process.

bashTrap.sh:

#!/bin/bash

function trigger_sigint ()
{
  echo "This is a response to interrupt signal SIGINT(2)";
}
function trigger_sigcont ()
{
  echo -e "\nThis is a response to signal SIGCONT(18)";
}
function trigger_sigusr1 ()
{
  echo "This is a response to user signal SIGUSR1(10)"
}

trap trigger_sigint SIGINT
trap trigger_sigcont SIGCONT
trap trigger_sigusr1 SIGUSR1

while [[ true ]] ; do
  sleep 1;
done
Different response to different signals given by the user to the background process

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads