DEV Community


Posted on • Updated on

Block CTRL-C in bash script

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


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


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

Top comments (0)