Skip to content

Instantly share code, notes, and snippets.

@gcoda
Created August 22, 2017 22:23
Show Gist options
  • Save gcoda/2340d3efe47f6e0de9fce046ca199bd6 to your computer and use it in GitHub Desktop.
Save gcoda/2340d3efe47f6e0de9fce046ca199bd6 to your computer and use it in GitHub Desktop.
Proper Ctrl + C on infinite bash loop
#!/usr/bin/env bash
#The exit status of a command that terminated because it received a signal shall be reported as greater than 128.
#For example if you interrupt a command with control-C the exit code will be 130, because SIGINT is signal 2 on Unix systems. So:
#source: https://unix.stackexchange.com/questions/42287/terminating-an-infinite-loop
while [ 1 ]; do
echo 1 #do the thing
sleep 1
test $? -gt 128 && break;
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment