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 examine files on Linux Linux provides very useful options for viewing file attributes, such as owners and permissions, as well as file content. By Sandra Henry Stocker Oct 24, 2024 6 mins Linux how-to 8 easy ways to reuse commands on Linux Typing the same command again and again can become tiresome. Here are a number of ways you can make repeating commands – or repeating commands but with some changes – a lot easier than you might expect. By Sandra Henry-Stocker Oct 15, 2024 5 mins Linux news SUSE Edge upgrade targets Kubernetes and Linux at the edge SUSE Edge 3.1 includes a new stack validation framework and an image builder tool that are aimed at improving the scalability and manageability of complex Kubernetes and Linux edge-computing deployments. By Sean Michael Kerner Oct 15, 2024 6 mins Edge Computing Linux Network Management Software how-to Lesser-known xargs command is a versatile time saver Boost your Linux command line options and simplify your work with xargs, a handy tool for a number of data manipulation tasks. By Sandra Henry Stocker Oct 11, 2024 6 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe