Loops and iterations in Bash

Bash (Bourne Again SHell) is one of the most popular and powerful shells available on Unix and Linux operating systems. One of the fundamental features of Bash is its ability to perform iterations or loops, which allow you to automate repetitive commands and processes. In this article, we'll explore the main types of iteration in Bash: the for loop, the while loop, and the until loop. We'll also look at some variations and advanced uses of each.

The for loop

The for loop is probably the most commonly used type of iteration in Bash. It is used to iterate over a series of values, such as items in a list, files in a directory, or numbers in a range.

Basic Syntax:


for variable in list
do
    commands
done

Example 1: Iterating over a list of words


for fruit in apple banana cherry
do
    echo "Fruit: $fruit"
done

This script prints:


Fruit: apple
Fruit: banana
Fruit: cherry

Example 2: Iterating over files in a directory


for file in /path/to/directory/*
do
    echo "Found file: $file"
done

Example 3: Iterating over a range of numbers


for i in {1..5}
do
    echo "Number: $i"
done

This script prints:


Number: 1
Number: 2
Number: 3
Number: 4
Number: 5

The while loop

The while loop executes commands as long as a condition remains true. It is useful when you don't know in advance how many iterations are needed.

Basic Syntax:


while [ condition ]
do
    commands
done

Example 1: Loop that counts up to a certain number


counter=1
while [ $counter -le 5 ]
do
    echo "Counter: $counter"
    counter=$((counter + 1))
done

This script prints:


Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 5

Example 2: Reading a file line by line


while IFS= read -r line
do
    echo "Line: $line"
done < "file.txt"

This example reads each line of "file.txt" and prints it to the screen.

The until loop

The until loop is similar to the while loop but executes commands as long as the condition remains false. It is, therefore, the inverse of the while loop.

Basic Syntax:


until [ condition ]
do
    commands
done

Example: Similar to the while loop example but with a different approach


counter=1
until [ $counter -gt 5 ]
do
    echo "Counter: $counter"
    counter=$((counter + 1))
done

This script also prints:


Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 5

Exiting loops: break and continue

  • break: Stops the execution of the loop.
  • continue: Skips the current iteration and moves to the next one.

Example of Using break and continue:


for i in {1..5}
do
    if [ $i -eq 3 ]; then
        continue
    fi
    if [ $i -eq 4 ]; then
        break
    fi
    echo "Number: $i"
done

This script prints:


Number: 1
Number: 2

Conclusion

Loops in Bash are essential tools for automating and managing repetitive tasks. The for, while, and until loops offer flexibility and power to manipulate data, iterate over files and lists, and much more. Learning to use them effectively can significantly improve your scripting skills and simplify many daily operations on Unix and Linux systems.

Back to top