[SOLVED] About Traps in bash script
by ddenial from LinuxQuestions.org on (#53DNH)
Hello All
I'm testing traps in Linux, and hence I've some doubts. Following is script for testing traps.
Code:#!/bin/bash
set -euo pipefail
echo "Script PID: $$"
function CleanUp {
echo
echo Oops I just got killed
exit 2
}
trap 'CleanUp' SIGHUP SIGINT SIGQUIT SIGPIPE SIGTERM EXIT
while true ; do
sleep 1
doneOutput:
Code:$ ./test.sh
Script PID: 16467
^C
Oops I just got killed
Oops I just got killedMy doubts are:


I'm testing traps in Linux, and hence I've some doubts. Following is script for testing traps.
Code:#!/bin/bash
set -euo pipefail
echo "Script PID: $$"
function CleanUp {
echo
echo Oops I just got killed
exit 2
}
trap 'CleanUp' SIGHUP SIGINT SIGQUIT SIGPIPE SIGTERM EXIT
while true ; do
sleep 1
doneOutput:
Code:$ ./test.sh
Script PID: 16467
^C
Oops I just got killed
Oops I just got killedMy doubts are:
- What is 'set -euo pipefail'? I've seen most bash scripts start with this line. I know 'set -u', which makes mandatory to declare a variable before using it. But what about -e and -o pipefail? Read man pages, but couldn't understand.
- Why I'm getting multiple trap echo lines?
- How do I get exit code based on the type of interrupt signal rather than the exit code which I have specified explicitly (like 2 here)?