Bash While Loop Examples: For Loops, Until Loops, And More


Bash programming comprises three kinds of loops: the for loop, the while loop, and the until loop. The idea of all the loops is to repeatedly execute some code as long as some specific parameters are met. 

The Bash for loop operates differently from the for loops in other programming languages you may be familiar with. With it, you can iterate over a series of “words” in a string. 

On the other hand, the while loop executes the code under it if the control expression (or simply, the “condition”) is true. The loop stops executing the code when the condition becomes false, or there is an explicit break in the code.

The until loop is comparable to the while loop, with the only difference being that the code keeps executing while the control expression is deemed false.

In this post, we discuss how to use the Bash while loop with examples.

Understanding the While Loop Syntax

Take a look at the simple syntax of the while loop:

while [ condition ] 

The beginning and end of a while loop are defined by the do and done keywords, respectively. The execution/termination condition is defined inside the square brackets at the beginning of the loop.

Examples of Bash While Loop

To run the examples below, create a Bash file with the .sh extension and paste the code inside it. You can then run the code by typing “bash” followed by the filename (for example, “”) in the terminal and hitting enter. 

#1 Iterating a Fixed Number of Times


# Initializing a counter variable
# Making a while loop 
while [ $counter -le 3 ]
    # Printing the counter value in every iteration 
    echo “Iteration count: $counter”
    # Incrementing the value 
    (( counter++ ))

The above while loop iterates over the code block three times and prints the statement every time. Running the script will generate the following outcome:

Iteration count: 1
Iteration count: 2
Iteration count: 3


#2 Using the “Break” Statement

The break statement allows you to terminate the loop if a specific condition is met. Create a Bash file with the following code:


# Initializing a counter variable
# Making a loop 
while [ $counter -le 5 ]
    # Checking the counter value
    if [ $counter == 3 ]
        echo “Ending the loop”       
    # Printing the counter value
   echo “Iteration number $counter”
    # Incrementing the value
    (( counter++ ))


The above while loop iterates over the code three times, but the counter value is only printed two times since the break statement terminates the loop when the counter reaches value “3.” Running the script will generate the following outcome:

Iteration number 1
Iteration number 2
Ending the loop


#3 Removing a Step with the Continue Statement


# Initializing the counter
# Iterate the loop four times
while [ $counter -le 4 ]
    # Increment counter value
    (( counter++ ))

      # Check counter value
    if [ $counter == 3 ]
    # Printing counter value
    echo ” Iteration number $counter”


The above code will iterate through the loop four times, but when the counter value reaches three, the continue value skips the rest of the code, jumps back to the beginning, and checks the condition.

The output for the code is:

Iteration number 1
Iteration number 2
Iteration number 4
Iteration number 5


#4 Reading a File


# Checking whether a command-line argument is provided 
if [ $# -gt 0 ]; then 

    # Assigning the filename from the provided value

    # Reading the file
    while read line; do
        # Printing every line
        echo $line
    done < $filename
    # If there is no argument in the command line, printing a message
    echo “No argument provided.”

Assuming that you paste this code in a file named “,” running the command “bash ReadingFileWithWhile.bash” will return “No argument provided.”

However, if you run the same command and pass a text file to it, the output will look like this:

$ bash ReadingFileWithWhile.bash names.txt


#5 Writing Text into a File

The following code will ask for a filename from the user and write the provided text to it:

#! /bin/bash

echo -n “Enter the name of the file you want to create: “
# Reading the provided name
read filename
# Reading the text entered into the terminal
while read line
    echo $line >> $filename

To end the loop reading the content, you must press Ctrl+D.

#6 Creating an Infinite Loop

Writing an infinite loop can be a suitable solution for many problems. Making an infinite while loop is as simple as not mentioning a termination condition. However, you will need to use an exit statement to stop the loop as necessary. 

The following loop will iterate six times, and when the value becomes six, the exit statement will stop the loop:


# Initialize the counter
# Make the infinite loop
while :
    printf “Iteration number: $counter\n”
    if [ $counter == 2 ]
        echo “level 1”
    elif [ $counter == 4 ]
        echo “level 2”
    elif [ $counter == 6 ] 
        echo “level 3”
        exit 0
    # Increment counter


The output of the script looks like this:

Iteration number: 1
Iteration number: 2
level 1
Iteration number: 3
Iteration number: 4
level 2
Iteration number: 5
Iteration number: 6
level 3



In this guide, we discuss a handful of the different ways you can use while loops in Bash. There are other ways to use the while loop, and you can solve many different problems with it.

Now that you know the fundamentals of the while loop, you can implement it in your Bash scripts.