How to terminate an infinite loop in Bash?

How to terminate an infinite loop in Bash?

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: You can stop and put your job in background while it’s running using ctrl + z. Then you can kill your job with: Where [1] is your job number.

When to remove exec from a shell script?

Remove it if you only want to call the command as a subprocess instead. exec transfers control of the PID over to the program you’re exec’ing. This is mainly used in scripts whose sole purpose is to set up options to that program. Once the exec is hit, nothing below it in the script is executed.

What’s the best way to kill an infinite loop?

So: You can stop and put your job in background while it’s running using ctrl + z. Then you can kill your job with: Where [1] is your job number. I would say it might be best to put your infinite loop in a script and handle signals there. Here’s a basic starting point.

What is the exit code for terminating a command in Bash?

If the command was terminated by a signal the exit code will be 128 + the signal number. From the GNU online documentation for bash: For the shell’s purposes, a command which exits with a zero exit status has succeeded. A non-zero exit status indicates failure.

When do you use an infinite while loop?

Infinite while loop. An infinite loop occurs when the condition will never be met, due to some inherent characteristic of the loop. There are a few situations when this is desired behavior. For example, the menu driven program typically continue till user selects to exit his or her main menu (loop). To set an infinite while loop use:

How to end infinite loop with any key pressed?

#!/bin/bash count=0 while : ; do # dummy action echo -n “$a ” let “a+=1” # detect any key press read -n 1 keypress echo $keypress done echo “Thanks for using this script.” exit 0 You need to put the standard input in non-blocking mode. Here is an example that works: