One Line Infinite “while” Loop in Bash [4 Examples]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

An infinite loop executes a set of commands repeatedly for an endless time. The while loop consists of a block of code, and the condition is evaluated before each iteration. If the condition is true, the block of code is executed; otherwise not. It terminates the loop and the next set of commands after the loop are executed. One line while loop command allows the user to run any infinite loop from the terminal. Here I will explore one line infinite while loop in Bash.

4 Examples of One Line Infinite “while” Loop

One line infinite while loop does a set of works like taking the user name then printing a greeting, using command substitution in another command line, nest with another loop. Follow the below section to learn more about these examples:

1. One Line Infinite “while” Loop in Bash

The initiation of the while loop offers the simplest approach to define an infinite loop in one line where only the loop condition is defined. Here is one line bash command of an infinite while loop:

while true; do echo “Hello From Infinite Loop”; sleep 1; done
EXPLANATION

while true; initiates an infinite while loop as the condition is constantly true. Then the echo command prints a message on the terminal, then the sleep command pauses the loop for 1 second before the next iteration.

Infinite while loop has printed a message multiple times.

2. One Line Infinite “while” Loop with Count Number

To count the execution number of a process, incorporate a counter variable with the while loop. Here is a bash script on one line infinite while loop with count number:

c=1; while true; do echo "Hello $c"; ((c++)); sleep 1; done
EXPLANATION

The c=1 assigns 1 to variable c before initiating the infinite while loop. In the while loop, the echo command prints a message with the value of c, at the same time, the value of c is increased by one.

The while loop has printed a message with a count number.

3. User Input in One Line Infinite “while” Loop

The read command reads input from users and processes it within any loop. Here is a one line command of infinite while loop that will take input from the user and then print a greeting along with user input:

while true; do read -p "Name: " in; echo "Welcome $in"; done
EXPLANATION

At first, the read command takes the name from the user and then prints a message incorporating it with the name.

The while loop has taken input from user and printed a greeting.

4. Command Substitution in One Line Infinite “while” Loop

Command substitution stores the output of a command and replaces the output with the command. It offers the flexibility to use the output of a command inside another command. Here is a one line infinite while loop with command substitution:

while true; do echo "Current date: $(date)"; sleep 1; done
EXPLANATION

The echo command prints the current date where $(date) is a command substitution. It gets replaced by the output of the date command.

The one line infinite loop has printed current date with time on each iteraion.

One Line Infinite “until” Loop

The until loop executes a set of tasks repeatedly as long as the loop condition remains false. To create an infinite until loop, put false inside the loop condition. Here is a one-line bash command to print a message continuously:

until false; do echo "Press [Ctrl+C] to exit."; sleep 1; done
EXPLANATION

until false; marks the beginning of an infinite while loop. Then the echo command prints a message on the terminal.

The infinite until loop has printed a text on each iteraion.

One Line-Nested Infinite Loop

Nesting means placing one loop inside another loop. The outer loop contains the inner loop. The loops can be any type. Here is a one line bash command to create a nested loop where the inner and the outer loop are while loop:

while true; do while true; do echo "Nested Infinite Loop"; sleep 1; done; done
EXPLANATION

while true; do while true; marks the initiation of a nested loop where the outer loop is the while loop and the inner loop is the while loop. The echo command prints a message on each iteration.

Nested while loop printed a message on the terminal.

Conclusion

In conclusion, one line infinite loop in Bash gives users the means to develop a code concisely and run it from the command line interface or terminal. This approach offers the most efficient way to run a set of commands repeatedly.

People Also Ask

How do you make an infinite loop in Bash?

To make an infinite loop in Bash, set the condition as true in the while loop or skip the iteration condition in the for loop. In these cases, the loop will execute continuously unless any command or operation explicitly interrupts it.

Which command will stop an infinite loop?

Pressing CTRL+C on the keyboard will stop an infinite loop. It sends a signal to the program to stop executing the infinite loop.

What is the main purpose of the infinite loop?

The main purpose of the infinite loop is to execute a set of commands continuously until any interruption occurs. But in a general context, an infinite loop appears due to a logical error in the loop condition.

Is an infinite loop always true?

Yes, the condition of an infinite loop is always true. For this reason, the infinite loops repeat endlessly.

What are the disadvantages of infinite loop?

Some disadvantages of the infinite loop are:

  1. Overconsumption of resources.
  2. Difficulties in Debugging.

Related Articles


<< Go Back to “while” Loop in Bash | Loops in Bash | Bash Scripting Tutorial

5/5 - (1 vote)
Susmit Das Gupta

Hello everyone. I am Susmit Das Gupta, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Mechanical Engineering graduate from Bangladesh University of Engineering and Technology. Besides my routine works, I find interest in going through new things, exploring new places, and capturing landscapes. Read Full Bio

Leave a Comment