You can set up a signal handler using trap:
trap 'myFunction arg1 arg2 ...' SIGINT;
I suggest keeping your script abortable overall, which you can do by using a simple boolean:
#!/bin/bash # define signal handler and its variable allowAbort=true; myInterruptHandler() { if $allowAbort; then exit 1; fi; } # register signal handler trap myInterruptHandler SIGINT; # some commands... # before calling the inner program, # disable the abortability of the script allowAbort=false; # now call your program ./my-inner-program # and now make the script abortable again allowAbort=true; # some more commands...
In order to reduce the likelihood of messing up with allowAbort, or just to keep it a bit cleaner, you can define a wrapper function to do the job for you:
#!/bin/bash # define signal handler and its variable allowAbort=true; myInterruptHandler() { if $allowAbort; then exit 1; fi; } # register signal handler trap myInterruptHandler SIGINT; # wrapper wrapInterruptable() { # disable the abortability of the script allowAbort=false; # run the passed arguments 1:1 "$@"; # save the returned value local ret=$?; # make the script abortable again allowAbort=true; # and return return "$ret"; } # call your program wrapInterruptable ./my-inner-program
trap your-ctrlc-handler-function SIGINT