How to loop forever in bash

There are several ways to loop “forever” on Linux commands. In practice, this means only until you decide to stop looping. You might do this while typing commands on the command line, or, more likely, within your scripts. It’s quite easy. This post provides some examples of how to do this.

The for and while commands will provide what you need. There are only a few things you have to keep in mind with respect to syntax and techniques.

Using while

One of the easiest forever-loops involves using the while command followed by the condition “true”. You don’t have to bother with logic like while [ 1 -eq 1 ] or similar tests. The while true test means the loop will run until you stop it with CTRL-C, close the terminal window, log out, or restart the system. After all, true is always true. Here’s an example:

$ while true
> do
> echo Keep running
> sleep 3
> done
Keep running
Keep running
Keep running
^C

The command in the example above will continue running, echo “Keep running” after every three-second break. Try “while false” and it won’t loop even once!

$ while false
> do
> echo Keep running
> sleep 3
> done
$

You can do the same thing with a “while :” test if you prefer. The key here is that the colon always yields success. Like “while true”, this test will never fail and the loop just keeps running. The syntax is just a bit less obvious to anyone who isn’t used to using it.

while :
do
echo Keep running
sleep 3
done

If you’ve inserted an infinite loop into a script and want to remind the person who is using it how to exit the script, you can always provide a little guidance as in the example below.

while :
do
echo Keep running
echo "Press CTRL+C to exit"
sleep 3
done

You can do the same thing in a single line by separating the lines shown above with semicolons as shown in the example below.

while true; do echo Keep running; echo 'Press CTRL+C'; sleep 3; done

The command below would work the same. I prefer while true because it seems obvious, but any of the examples shown will work as does this one:

while [ 1 ]
do
echo Keep running
echo "Press CTRL+C to exit"
sleep 3
done

Using for

The for command also provides an easy way to loop forever. While not quite as obvious as the while true option, the syntax is reasonably straightforward. You can just replace the parameters in a normal for loop (the start, condition and increment values) that would generally look something like this:

$ for (int i = 0; i < 5; i++)

Instead, use the for command with no such values as in the example below.

$ for (( ; ; ))
> do
> echo Keep your spirits up
> echo “Press CTRL+C to exit”
> sleep 3
> done
Keep your spirits up
“Press CTRL+C to exit“
Keep your spirits up
“Press CTRL+C to exit“
Keep your spirits up
“Press CTRL+C to exit“

And, obviously, press CTRL-C when you want to exit the loop.

Why loop forever?

Of course, you’re not ever going to want to loop forever, but having loops run until you stop them can often be very useful. The script below runs until 5:00 p.m. It uses the date command with the +%H option to check the time, then reminds you that it’s time to go home and exits. The sleep 60 command was included to limit the time checking to once a minute. Otherwise, this script could gobble up a lot of CPU time.

#!/bin/bash

while true
do
if [ `date +%H` -ge 17 ]; then
echo Time to go home
exit # exit script
else
sleep 60
fi
done

If you want to exit the loop instead of exiting the script, you can use a break command instead of an exit command as shown in the example below.

#!/bin/bash

while true
do
if [ `date +%H` -ge 17 ]; then
break # exit loop
fi
echo Time to go home
done

Wrap-up

Looping forever is easy. Determining what commands you want to run repeatedly and the conditions under which you want them to stop running are the only challenging parts of putting these forever loops into use.

Source:: Network World