Linux / UNIX: Bash Loop Forever

In a Linux or Unix shell, you can create an infinite loop using the while loop. The syntax for the while loop is as follows:

while true; do
[command1]
[command2]
...
done

Here, true is a command that always returns a successful exit status (0), so the loop will continue to execute indefinitely until it is interrupted by a user or by a system event. The commands between do and done will be executed repeatedly in the loop.

For example, the following script will print the string “Hello, World!” every 5 seconds:

while true; do
echo "Hello, World!"
sleep 5
done

Leave a Comment