Credit: tine ivanic 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. Related content how-to How to find files on Linux There are many options you can use to find files on Linux, including searching by file name (or partial name), age, owner, group, size, type and inode number. By Sandra Henry Stocker Jun 24, 2024 8 mins Linux opinion Linux in your car: Red Hat’s milestone collaboration with exida With contributions from Red Hat and critical collaborators, the safety and security of automotive vehicles has reached a new level of reliability. By Sandra Henry Stocker Jun 17, 2024 5 mins Linux how-to How to print from the Linux command line: double-sided, landscape and more There's a lot more to printing from the Linux command line than the lp command. Check out some of the many available options. By Sandra Henry Stocker Jun 11, 2024 6 mins Linux how-to Converting between uppercase and lowercase on the Linux command line Converting text between uppercase and lowercase can be very tedious, especially when you want to avoid inadvertent misspellings. Fortunately, Linux provides a handful of commands that can make the job very easy. By Sandra Henry Stocker Jun 07, 2024 5 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe