DEV Community

hamza72x
hamza72x

Posted on • Updated on

Block CTRL-C in bash script

You can block / control ctrl-c in bash script by following:

#!/bin/bash

trap ctrl_c INT

function ctrl_c() {
    echo "Cleaning up some stuff before quiting..."

    # Do your stuff here
    # For example: kill any background task that you ran

    # Don't forget to exit after your cleanup is done
    # otherwise the script will not be stopped

    exit
}

# here goes your rest of the script
Enter fullscreen mode Exit fullscreen mode

Top comments (0)