An infinite loop in Bash or any other programming language refers to a loop that is continuous i.e., its terminating condition is never met or its executing condition forever stays true. Such loops in any programming language are very simple to write. Whether it is a “for” loop or a “while” loop, it can be made infinite with very slight tweaking in its normal syntax.

In this article, we will be sharing with you the different ways on how you can conveniently make the “for” and “while” loops infinitely in Bash in Linux Mint 20.

Bash Infinite Loop Example Scripts in Linux Mint 20:

There are different ways of working with infinite loops in Bash, and the example scripts demonstrating these are described below:

Note: You can access all the Bash scripts discussed in this article in our Home Directory named InfiniteLoop.sh.

Script # 1: “While” Loop using the “:” Command in Bash in Linux Mint 20:

In this example, we will be creating a never-ending “while” loop by pairing it up with the “:” command in Bash in Linux Mint 20. Just copy the following script shown in the image in a Bash file.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/b1.png" data-lazy- height="152" src="data:image/svg xml,” width=”798″>

As shown in the Bash script above, we have created a “while” loop followed by the “:” command. This command is an alternative to the “true” command, which means that no matter what the situation is “while” loop will always execute. Inside this “while” loop, we have simply printed a sample message that says, “Keep Running”. Afterward, we have the “sleep” command, it waits for 1 second before printing every next message on the terminal.

Once this Bash script is written, we will execute it with the command shown below:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/b2.png" data-lazy- height="24" src="data:image/svg xml,” width=”436″>

When the said script is executed, you will notice unending messages saying, “Keep Running”, being displayed on your terminal, as shown in the following image. These messages will only stop if you press Ctrl C. Otherwise, this loop will just go on and on.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/b3.png" data-lazy- height="404" src="data:image/svg xml,” width=”471″>

Script # 2: “While” Loop using the “true” Command in Bash in Linux Mint 20:

In this example, we will be creating a never-ending “while” loop by pairing it up with the “true” command in Bash in Linux Mint 20. Just simply copy the script shown in the image in a Bash file. As you can notice, the script is exactly the same as the one we created in the first scenario. However, the only difference is that this time, we have replaced the “:” command with the “true” command. Nonetheless, it will serve the exact same purpose.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/b4.png" data-lazy- height="154" src="data:image/svg xml,” width=”800″>

We will execute this script with the same “bash” command, and we will notice a never-ending series of messages on our terminal, which will only terminate once we press Ctrl C, as shown in the image below:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/b5.png" data-lazy- height="167" src="data:image/svg xml,” width=”487″>

Script # 3: One Liner “While” Loop using the “:” Command in Bash in Linux Mint 20:

You might observe that Script #1 and 3 are unnecessarily lengthy. Well, both of these scripts can be squeezed into a one-liner command. Just copy the script shown in the image below:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/b6.png" data-lazy- height="101" src="data:image/svg xml,” width=”798″>

The script shown in the image above is the exact replication of Script # 1. However, instead of writing every command in a different line, we simply separated them using semi-colons.

When we execute this script, we will get the exact same results as we got after executing Script #1. This can be seen from the image shown below:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/b7.png" data-lazy- height="146" src="data:image/svg xml,” width=”470″>

Script # 4: One Liner “While” Loop using the “true” Command in Bash in Linux Mint 20:

Similarly, we can squeeze Script #2 in a one-liner command. Just copy the script shown in the image below:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/b8.png" data-lazy- height="101" src="data:image/svg xml,” width=”803″>

It can be observed that the script shown in the image above is the exact replication of Script #2. Again, the only difference is that instead of writing every command in a different line, we simply separated them using semi-colons.

When we execute this script, we will get the exact same results as we got after executing Script #2. This can be seen from the image shown below:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/b9.png" data-lazy- height="147" src="data:image/svg xml,” width=”510″>

Script # 5: For Loop without any Parameters in Bash in Linux Mint 20:

This example is different from Scripts #1 to 4 because instead of using the “while” loop, we are going to create an infinite “for” loop. Just copy the script shown in the image below:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/b10.png" data-lazy- height="150" src="data:image/svg xml,” width=”799″>

The task that we are going to perform inside the “for” loop is the same as we did with the scripts discussed above. However, instead of using the “while” loop, we have used the “for” loop without any conditions or parameters. It is always executed since its condition is considered “true” by default.

We will execute this script with the same “bash” command, and we will notice a never-ending series of messages on our terminal, which will only terminate once we press Ctrl C, as shown in the image below:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/b11.png" data-lazy- height="195" src="data:image/svg xml,” width=”509″>

Conclusion:

In this article, we taught you five different ways of implementing infinite loops in Bash. These loops will keep on running forever since no terminating condition is specified, or even if there is, it is never going to meet. Therefore, if you want to put an end to this unending loop, you will either have to make use of a “break” statement with a specific condition within this loop or during the execution of such script, you have to simply press Ctrl C as we have discussed in all of our examples.

About the author

<img alt="Aqsa Yasin" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/Author-Image-150×150.jpg6036760f7aba8.jpg" height="112" src="data:image/svg xml,” width=”112″>

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.