while true; do foo; sleep 2; done

By the way, if you type it as a multiline (as you are showing) at the command prompt and then call the history with arrow up, you will get it on a single line, correctly punctuated.

$ while true
> do
>    echo "hello"
>    sleep 2
> done
hello
hello
hello
^C
$ <arrow up> while true; do    echo "hello";    sleep 2; done
Answer from Stefano Borini on Stack Overflow
🌐
nixCraft
cyberciti.biz › nixcraft › howto › bash shell › bash infinite loop examples
Bash Infinite Loop Examples - nixCraft
February 3, 2025 - This is a loop that will forever print “Press [CTRL+C] to stop..”. Please note that : is the null command. The null command does nothing and its exit status is always set to true. You can modify the above as follows to improve the readability: #!/bin/bash while true do echo "Press [CTRL+C] to stop.." sleep 1 done · A single-line bash infinite while loop syntax is as follows:
🌐
Spiceworks
community.spiceworks.com › software & applications
Create Infinite Loop In Bash - Software & Applications - Spiceworks Community
April 20, 2020 - I want to run a command for ever in the Linux bash. Also I want to put 60 seconds between command execution.
🌐
Network World
networkworld.com › home › blogs › unix as a second language
How to loop forever in bash | Network World
February 27, 2025 - 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
🌐
iO Flood
ioflood.com › blog › bash-infinite-loop
Creating an Infinite Bash Loop: Linux Shell Script Syntax
December 4, 2023 - In this example, we’ve used the ‘while’ loop with the condition set to ‘true’. This means the loop will keep running and echoing ‘This is an infinite loop’ until it’s explicitly stopped by the user. But Bash’s infinite loop capabilities go far beyond this.
Find elsewhere
🌐
DaniWeb
daniweb.com › hardware-and-software › linux-and-unix › threads › 522438 › create-infinite-loop-in-bash
shell scripting - Create Infinite Loop In Bash | DaniWeb
When I was first using Unix one of our programmers wrote a Bash script something like: While 1 Do Something · He eventually overloaded the system and it crashed. ... while true; do echo "Running..."; sleep 60; done This runs forever, printing "Running..." every 60 seconds. No script file needed, just drop it in the terminal and you're good. ... Create an infinite loop in Bash to run commands continuously without stopping.
🌐
GeeksforGeeks
geeksforgeeks.org › linux-unix › bash-scripting-while-loop
Bash Scripting - While Loop - GeeksforGeeks
January 2, 2024 - This is how we can read the contents of the file using a while loop in BASH. To create an infinite loop using a while loop statement. We don't need to put any condition in the while loop and hence the loop iterates infinitely.
🌐
LabEx
labex.io › tutorials › linux-how-to-stop-bash-script-infinite-loop-435107
How to stop bash script infinite loop | LabEx
An infinite loop is a sequence of instructions in a bash script that repeats indefinitely because the loop's termination condition is never met.
🌐
Quora
quora.com › What-is-the-best-way-to-do-an-infinite-loop-in-a-shell-script
What is the best way to do an infinite loop in a shell script? - Quora
Answer (1 of 3): “best”? Depends on what you mean by “best”. Shortest? Will iterate the fastest? Clearest code? Neatest? How about: [code]while :; do :; done [/code]The “:” (= a single colon) command is essentially a no-op - the shell will still evaluate redirections and any arguments ...
🌐
Vultr
docs.vultr.com › how-to-use-while-loop-in-bash
Bash While Loop Tutorial: Syntax, Examples & Best Practices | Vultr Docs
October 18, 2024 - Follow the steps below to create an infinite while loop that continuously runs unless you stop it using Ctrl + C. Create a new infinite.sh script. ... Add the following contents to the infinite.sh file.
🌐
Warp
warp.dev › terminus › bash-while-loop
Warp: How To Use The While Loop In Bash
July 30, 2024 - To create an infinite while loop in Bash, you must define a condition that never evaluates to false.
🌐
Hostinger
hostinger.com › home › tutorials › how to use the bash for loop: syntax and examples
What is a bash for loop? Practical examples and syntax
December 22, 2025 - Since there’s no termination condition to meet, the loop will continue until the user stops it. A Bash for loop lets you create a loop that skips a specific value and continues running afterward.
🌐
Steve's Data Tips and Tricks
spsanderson.com › steveondata › posts › 2025-03-28
Mastering Linux Shell Loops: While and Until Commands – Steve’s Data Tips and Tricks
March 28, 2025 - This script creates a menu that repeats until the user selects the exit option (0). It demonstrates: - Using an infinite loop with while true - Breaking out of the loop with the break command - Using a case statement for menu selection · A common use of loops is to process files line by line. Here’s how you can do this: #!/bin/bash # Reading a file line by line while read line do echo "Line: $line" done < /etc/passwd
🌐
Linux Hint
linuxhint.com › creating-bash-infinite-loop-by-example-scripts
Creating Bash Infinite Loop by Example Scripts – Linux Hint
Every loop has a finite lifespan and depending on the loop, it ends when the condition is either true or false. The bash infinite loop is simply a series of instructions that loops indefinitely. It has no ending condition, a condition that is never met, or a condition that stimulates a new ...