Johannes Ernst
Johannes Ernst

Reputation: 3186

How to print a message and continue after trap'ing a signal in bash?

I'd like to to do this in bash:

trap "echo Don\'t do that!" 2 3

which works just fine, except that I want the script to continue. How can I do that? If I leave the command as a blank string, the script continues, but does not print anything. Can I have both printing message and continuing?

Upvotes: 2

Views: 1844

Answers (1)

DopeGhoti
DopeGhoti

Reputation: 767

With this script:

#!/bin/bash
trap 'echo "Whee!"' 3 2

echo "Setting up.."
sleep 5
echo "Done."

I get this output:

Setting up..
^CWhee!
Done.

..when I sent a ^C during the sleep command. The interrupt is sent; bash traps it and continues, but the interrupt is properly handled by sleep. Is this not what you want?

Upvotes: 4

Related Questions