Shell Scripting – Bash Trap Command
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.
To revert back or remove the trap, write the command :
$ trap - SIGINT
Example 2: STOP signal
Write the following command :
$ trap “echo This is a response to STOP signal” 20
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
If only the -p option is given, then it prints the piece of code we wrote associated with each signal.
$ trap -p SIGINT
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
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
- 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
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
Contact Us