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 Doing tricks on the Linux command line Linux tricks can make even the more complicated Linux commands easier, more fun and more rewarding. By Sandra Henry-Stocker Dec 08, 2023 5 mins Linux how-to Getting started on the Linux (or Unix) command line, Part 4 Pipes, aliases and scripts make Linux so much easier to use. By Sandra Henry-Stocker Nov 27, 2023 4 mins Linux how-to Getting started on the Linux (or Unix) command line, Part 3 Our Linux cheat sheet includes some of the most commonly used commands along with brief explanations and examples of what the commands can do. By Sandra Henry Stocker Nov 21, 2023 6 mins Linux how-to Getting started on the Linux (or Unix) command line, Part 2 Commands that provide help are essential. Here's a look at some of the help you can get from the Linux system itself. By Sandra Henry Stocker Nov 20, 2023 5 mins Linux Podcasts Videos Resources Events NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe