“while true” Loop in Bash [4 Cases]

While true loop in Bash is a kind of while loop where the condition is always true. It is a type of infinite loop as it runs indefinitely with the true condition. In this article, I will describe the basics of a while true loop with an example. Additionally, this article demonstrates how the while true loop works with the while loop statements (sleep, break) and logical operators (AND, OR).

Basics of “while true” Loop in Bash

In bash, while true loop is a conditional construct that uses while true statement to create an infinite loop. As the condition is always true here, the loop executes forever until it is explicitly terminated. This loop can execute a block of statements continuously without any interruption. The while true loop is useful where continuous monitoring and repetitive execution are required.

“while true” Loop Syntax and Example

The basic syntax of the while true loop in Bash is:

while true; do
  # Statements to be executed
done
EXPLANATION

while true: Starts the while loop, which will continue forever because the condition “true” always returns true.

do: Indicates the start of the statements that execute in every iteration of the loop.

# Statements to be executed: These statements will be executed over and over again as the condition always evaluates to true.

done: Marks the end of the while loop.

Now, see an example of a while true loop where the loop increments the number 0 by 1 in every iteration indefinitely:

#!/bin/bash
number=0
while true; do
  echo "$number"
  ((number++))
done
EXPLANATION

This script begins with a variable number with the value 0. While true; do starts the infinite loop and the ((number++)) increases the number by 1 after completing each iteration. The output of each loop is printed by the echo command.

while true loop example for incrementing number

As the condition always returns true, the loop is running forever by incrementing the initial value 0 by 1 in each iteration. You can exit out of the loop by pressing CTRL+C.

4 Cases of “while true” Loop in Bash

In this part, I will explain 4 different cases of the while true loop. How the while true loop works with the sleep and break statements of the while loop will be discussed here. Moreover, the behavior of the while true loop with the logical AND and OR operators will be addressed.

1. “while true” Loop With “sleep” Command

The sleep command pauses the statement from executing for a certain amount of time until the next statement is executed in the loop. In this example, the sleep 3 command in the while true loop will pause the execution for 3 seconds in every iteration. Follow the script below to see how the while true loop works with the sleep command:

#!/bin/bash
number=0
while true; do
   echo "Linuxsimply"
   ((number++))
   sleep 3
 done
EXPLANATION

The while true loop is initiated by taking a variable number with the value of 0 and incrementing it by 1 after completing each iteration with the ((number++)). The loop runs the string “Linuxsimply” for infinite times. After completing each loop, it pauses for 3 seconds before starting the next iteration because of the sleep 3 command.

while true loop with sleep command

This picture shows that the script keeps printing the string “Linuxsimply” every 3 seconds and the loop is running indefinitely.

2. “while true” Loop With “break” Command

The break command is used to exit out of any loop. It can break the infinite while true loop at a certain condition. The break statement gives the freedom to execute a loop until a specific condition is met. Here’s how the while true loop works with the break command in Bash:

#!/bin/bash
number=0
while true; do
  # Check if number is equal to 6
      if [ "$number" -eq 6 ]; then
         echo "Exit the loop."
         break
      fi
    # Print the current value of the number
    echo "$number"
    ((number++))
    sleep 1
done
EXPLANATION

The initial variable value is 0. Then while true statement starts the infinite loop. The "$number" -eq 6 condition checks if the number is equal to 6.  The echo command prints the current value of the number and increases the number by 1 after completing every iteration. The loop continues until the number becomes 6. When the number is 6, the infinite loop gets terminated due to the break statement. The seep 1 command introduces a 1-second delay between each iteration of the loop.

while true loop with break command

As you can see, after each loop, the value of number=0 is incremented by 1 and the loop is repeated until the number equals 6. When the condition is met, the break statement terminates the infinite loop.

3. “while true” Loop With “AND” Operator

In the while true loop, you can employ the AND operator (&&) to test multiple conditions. The AND operator only executes the true output when all conditions are satisfied. In this example, the -lt (less than) and -ne (not equal to) are the conditional operators of the if statement to test the specified conditions.

Now, look at the bash script to see how the while true loop functions with the AND operator:

#!/bin/bash
number=0
while true; do
    # Check if the number is less than 6 and is odd
    if [ "$number" -lt 6 ] && [ "$((number % 2))" -ne 0 ]; then
        echo "Current value: $number"
   fi
    # Increment the number
    ((number++))
    sleep 1
done
EXPLANATION

The infinite loop is created with the while true statement, and then the conditions within the if statement check whether the number is less than 6 (“$number" -lt 6) and odd (“$((number % 2))" -ne 0) using the && operator. If both conditions are satisfied, the script prints the current value of the number and increments the number by 1 after completing every iteration.

while true loop with AND operator

Running the script with ./AND.sh, shows that the numbers 1,2, and 3 are printed as the numbers are odd and less than 6. To exit out of the infinite loop, type CTRL+C and press ENTER.

4. “while true” Loop With “OR” Operator

In this example, the OR (||) logical operator is used with the while true loop. The OR operator evaluates to true if at least one condition is satisfied. Here, within the while true loop, the two comparison operators -le (less than and equal to) and -ge (greater than and equal to) of the if statement are employed to check the conditions.

Follow the bash script to understand the behavior of the while true loop with the OR operator:

#!/bin/bash
number=0
while true; do
    # Check if number is less than and equal to 4 or greater and equal to 7
    if [ "$number" -le 4 ] || [ "$number" -ge 7 ]; then
        echo "$number"
    else
        echo "$number is greater than 4. Exit the loop."
        break
    fi
    ((number++))
    sleep 1
done
EXPLANATION

First, the number=0 is taken as input. Then the while true loop continues till at least one condition is satisfied. The two conditions are connected using the OR operator in the if condition. if [ "$number" -le 4 ] || [ "$number" -ge 7 ] checks if the number is less than and equal to 4 or greater and equal to 7. If the 1st condition is met and the number becomes greater than greater than 4, the while true loop terminates with the break command.

while true loop with OR operator

As you can see, the loop is terminated when the number becomes 5, which means the 1st condition is satisfied.

Conclusion

To wrap up, I have covered the fundamentals of while true loop including syntax and examples. Furthermore, a detailed explanation of 4 distinct cases of the while true loop is provided. Check the bash scripts thoroughly to understand the landscape of the bash while true loop.

People Also Ask

What is a while true loop in Bash?

A while true loop in Bash is a while loop that creates an infinite loop with the while true statement. This loop runs forever as the condition always evaluates to true.

What is the difference between a while loop and a while true loop in Bash?

The difference between a while loop and a while true loop in Bash is that the while loop relies on a certain condition, it runs as long as the condition is true whereas the while true loop is an infinite loop and runs forever as the condition always returns true.

Is there an alternative to while true for infinite loops?

Yes, there is an alternative to while true for infinite loops in bash which is while :. This has the same purpose as while true.

Can I create an infinite loop with while true?

Yes, you can create an infinite loop with the while true statement. This statement indicates that the condition is always true. That’s how it creates an endless loop.

How can I break out of an infinite loop created by while true?

To break out of an infinite loop created by while true, you can use a break statement inside the loop body. Additionally, you can use an if condition inside the loop, when the condition is satisfied, the break command will break the infinite loop.

What is the difference between a while true loop and a for loop?

Though both control the flow of statements, the key difference is that the while true executes its code indefinitely whereas the for loop executes its code only a limited number of times.

Related Articles


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

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
icon linux
Mitu Akter Mou

Hello, This is Mitu Akter Mou, currently working as a Linux Content Developer Executive at SOFTEKO for the Linuxsimply project. I hold a bachelor's degree in Biomedical Engineering from Khulna University of Engineering & Technology (KUET). Experiencing new stuff and gathering insights from them seems very happening to me. My goal here is to simplify the life of Linux users by making creative articles, blogs, and video content for all of them. Read Full Bio

Leave a Comment