Catch SIGINT in bash, handle AND ignore

bash

Solution

trap will return from the handler, but after the command called when the handler was invoked.

So the solution is a little clumsy but I think it does what is required. `trap handler INT` also will work.

trap 'echo "Be patient"' INT

for ((n=20; n; n--))
do
    sleep 1
done

Problem

Is it possible in bash to intercept a SIGINT, do something, and then ignore it (keep bash running). I know that I can ignore the SIGINT with ``` trap '' SIGINT ``` And I can also do something on the sigint with ``` trap handler SIGINT ``` But that will still stop the script after the `handler` executes. E.g. ``` #!/bin/bash handler() { kill -s SIGINT $PID } program & PID=$! trap handler SIGINT wait $PID #do some other cleanup with results from program ``` When I press ctrl+c, the SIGINT to program will be sent, but bash will skip the `wait` BEFORE program was properly shut down and created its output in its signal handler. Using @suspectus answer I can change the `wait $PID` to: ``` while kill -0 $PID > /dev/null 2>&1 do wait $PID done ``` This actually works for me I am just not 100% sure if this is 'clean' or a 'dirty workaround'.

Original source