How to Create an Infinite Loop in Bash

MD Aminul Islam Feb 02, 2024
  1. Create an Infinite Loop Using while With a Single Line of Commands in Bash
  2. Create an Infinite Loop Using while With Multiple Lines of Commands in Bash
  3. Create an Infinite Loop Using for in Bash
How to Create an Infinite Loop in Bash

We use infinite loops for various purposes. In this article, we will learn how to create an infinite loop in Bash using while and for.

Also, we will see some examples with proper explanations to make them easier to understand.

Create an Infinite Loop Using while With a Single Line of Commands in Bash

The most basic way to design an infinite loop is to provide a true condition to the while loop and don’t make the condition false inside the code block. The general format of the infinite while loop is:

while true; do YOUR BLOCK OF CODES; done

Below is a single line of code that is an infinite loop. Here is the code for our example:

while true; do echo "Hello World"; sleep 2; done

Here you can notice the sleep 2 part of the line. The purpose of this part of the line is to wait for 2 seconds so that the console doesn’t become full in 2 seconds.

After executing the code, you will get an output like the following:

Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World

Create an Infinite Loop Using while With Multiple Lines of Commands in Bash

This example just reorganizes the code structure from single line to multi-line. Below is the example code:

while true
do
   echo "Hello World"
   sleep 2
done

The above code will show the same result as our previous example. We used the line sleep 2 for the same purpose here.

After executing the code, you will get the same output as shown below:

Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World

Create an Infinite Loop Using for in Bash

This alternative way to create an infinite loop uses the for loop. All we need to do here is remove the condition part from the code.

The general syntax of the for loop is:

for(( INITIALIZE; CONDITION; INCREMENT )); do YOUR BLOCK OF CODES done

Let’s see the code for our example:

for(( i=0; ;++i ));
do
   echo "Hello World"
   sleep 2
done

This will also provide you with the same output as our previous examples. You can notice that we just removed the conditional part from the for loop, and we used the line sleep 2 for the same purpose as the above example.

After executing the code, you will get the same output as below:

Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World

Please note that all the code used in this article is written in Bash. It will only work in the Linux Shell environment.

MD Aminul Islam avatar MD Aminul Islam avatar

Aminul Is an Expert Technical Writer and Full-Stack Developer. He has hands-on working experience on numerous Developer Platforms and SAAS startups. He is highly skilled in numerous Programming languages and Frameworks. He can write professional technical articles like Reviews, Programming, Documentation, SOP, User manual, Whitepaper, etc.

LinkedIn

Related Article - Bash Loop