POSIX compatible UNIX systems like Linux supports standard signals as part of inter process communication. While some signals like SIGINT can be ignored, SIGKILL and SIGSTOP cannot be caught, blocked or ignored.

Bash provides a command “trap” to assign a signal handler. The following example script catches SIGINT signal that is generally received when user presses Ctrl+C key combination. A bash script will generally terminate on pressing Ctrl+C but it can chose to ignore the signal or do clean up before terminating using signal handler.

#!/bin/bash

trap mysighandler INT

mysighandler()
{
    echo 'You have pressed Ctrl+C.. Ignoring...'
}

for ((i=0;i<10;i++))
do
   echo Loop $i .. Sleeping for 2 seconds
   sleep 2s
done

The following is the output

Loop 0 .. Sleeping for 2 seconds
Loop 1 .. Sleeping for 2 seconds
^CYou have pressed Ctrl+C.. Ignoring...
Loop 2 .. Sleeping for 2 seconds
Loop 3 .. Sleeping for 2 seconds
^CYou have pressed Ctrl+C.. Ignoring...
Loop 4 .. Sleeping for 2 seconds
Loop 5 .. Sleeping for 2 seconds
Loop 6 .. Sleeping for 2 seconds
Loop 7 .. Sleeping for 2 seconds
^CYou have pressed Ctrl+C.. Ignoring...
Loop 8 .. Sleeping for 2 seconds
Loop 9 .. Sleeping for 2 seconds

In the above execution, Ctrl+C is pressed thrice. Yet the script ignores it continues to execute.