Skip to content

Instantly share code, notes, and snippets.

@RooSoft
Created January 1, 2020 16:18
Show Gist options
  • Save RooSoft/5c2a79e9fd9c6d4a50ccc4cef1d09bdc to your computer and use it in GitHub Desktop.
Save RooSoft/5c2a79e9fd9c6d4a50ccc4cef1d09bdc to your computer and use it in GitHub Desktop.
How to trap ctrl-c in a bash script
#!/bin/bash
ctrlc_count=0
function no_ctrlc()
{
let ctrlc_count++
echo
if [[ $ctrlc_count == 1 ]]; then
echo "Stop that."
elif [[ $ctrlc_count == 2 ]]; then
echo "Once more and I quit."
else
echo "That's it. I quit."
exit
fi
}
trap no_ctrlc SIGINT
while true
do
echo Sleeping
sleep 10
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment