Bash Script – Not Terminated After Pressing Ctrl+C

bash

I have a select menu with 4 options. I want that in case of pressing ctrl + c, remove some files before terminating the program. Here is my code:

#!/bin/bash

ctrl_c() {
     test -f directory/file.txt && rm directory/file.txt
}

trap ctrl_c INT

PS3='Please enter your choice: '
while true; do
    clear
    options=("Option 1" "Option 2" "Option 3" "Exit")
    select opt in "${options[@]}"
    do
        case $opt in
            "Option 1")
                echo "you chose choice $REPLY which is $opt"
                break
                ;;
            "Option 2")
                echo "you chose choice $REPLY which is $opt"
                break
                ;;
            "Option 3")
                echo "you chose choice $REPLY which is $opt"
                break
                ;;
            "Exit")
                break 2
                ;;
            *) echo "invalid option $REPLY";;
        esac
    done
read -p "Press [Enter] key to continue..."
done

but when I run this code and press ctrl + c, nothing happens and the program is not terminated, just ^c is typed. What's wrong?

Best Answer

Ctrl + C sends the SIGINT signal, but you are capturing that and assigning it to your ctrl_c function:

trap ctrl_c INT

So when you press Ctrl + C, your script will execute the function instead of quitting. If that's not what you want, remove the trap command.

If your objective is to run the ctrl_c function and then exit, you need to tell your script to exit explicitly:

ctrl_c() {
     test -f directory/file.txt && rm directory/file.txt
     exit
}

When you trap a signal, it is up to you to implement what you want your program to do in response to that signal.

This will however not work as expected unless the posix shell option is active (set -o posix) as signals are not delivered while a command is actively executing (see "Bash ignoring SIGINT trap when 'select' loop is running").

This means that you may want to use set -o posix before calling select (and then possibly set +o posix afterwards).

Related Question