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
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.
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
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.
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
At first, the read command takes the name from the user and then prints a message incorporating it with the name.
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
The echo command prints the current date where $(date)
is a command substitution. It gets replaced by the output of the date command.
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
until false;
marks the beginning of an infinite while loop. Then the echo command prints a message on the terminal.
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
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.
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:
- Overconsumption of resources.
- Difficulties in Debugging.
Related Articles
- “while true” Loop in Bash [4 Cases]
- 8 Examples of “while” Loop in Bash
- How to Read File Line Using Bash “while” Loop [8 Cases]
- How to Increment Number Using Bash “while” Loop [8 Methods]
- How to Use Bash Continue with “while” Loop [7 Examples]
- Exit “while” Loop Using “break” Statement in Bash [11 Examples]
- How to Use Bash One Line “while” Loop [8 Examples]
- How to Use “sleep” Command in Bash “while” Loop [6 Examples]
<< Go Back to “while” Loop in Bash | Loops in Bash | Bash Scripting Tutorial
FUNDAMENTALS A Complete Guide for Beginners