Americas

  • United States
sandra_henrystocker
Unix Dweeb

How to loop forever in bash on Linux

How-To
Jun 15, 20203 mins
Linux

There are a number of ways to loop forever (or until you decide to stop) on Linux and you can do this on the command line or within scripts.

The for and while commands make the job quite easy. There are only a few things to keep in mind with respect to syntax and tactics.

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 or log out. Here’s an example:

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

You can also do the same thing with while :. The key here is that the : always yields success so, like while true, this test doesn’t ever fail and the loop just keeps running.

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

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 add a hint using the echo command:

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

Using for

The for command also provides an easy way to loop forever. While not quite as obvious as while true, the syntax is reasonably straightforward. You just replace the parameters in a bounded loop that would generally look something like this “start with c equal to 1 and increment it until reaches 5” specification:

$ for (( c=1; c

with one that doesn’t specify any parameters:

$ for (( ; ; ))

With no start value, increment or exit test, this loop will run forever or until it is forcibly stopped.

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

Why loop forever?

In real life, you’re not ever going to want to loop forever, but running until it’s time to go home, the work is done or you run into a problem is not at all unusual. Any loop that is constructed as an infinite loop can also be set up to be exited depending on various circumstances.

This script would keep processing data until 5 p.m. or the first time it checks the time after 5 p.m.:

#!/bin/bash

while true
do
  if [ `date +%H` -ge 17 ]; then
    exit	# exit script
  fi
  echo keep running
  ~/bin/process_data     # do some work
done

If you want to exit the loop instead of exiting the script, use a break command instead of an exit.

#!/bin/bash

while true
do
  if [ `date +%H` -ge 17 ]; then
    break	# exit loop
  fi
  echo keep running
  ~/bin/process_data
done
… run other commands here …

Wrap-Up

Looping forever is easy. Specifying the conditions under which you want to stop looping takes a little extra effort.

sandra_henrystocker
Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.

More from this author