Loops in Shell Scripting (for, while, until) – Complete Guide with Examples

Loops in Shell Scripting A Complete Guide with Examples

When working with shell scripting loops, one of the biggest challenges is avoiding repetitive tasks. Imagine running the same command 50 times manually — inefficient, error-prone, and time-consuming. This is where loops in shell scripting become essential. Loops allow you to automate repetitive operations such as processing files, monitoring systems, or iterating over data. Whether you are building automation scripts or handling system tasks, mastering loops is a fundamental skill in bash scripting.

In this guide, you will learn:

  • What loops are in shell scripting
  • Types of loops: for loop, while loop, until loop
  • Real-world bash loop examples
  • Loop control using break and continue
  • Practical use cases for automation

Loops in shell scripting are control structures that automate repetitive tasks by executing commands multiple times based on conditions or lists. The three main types—for loop, while loop, and until loop—are used for iterating over known values, running condition-based operations, and waiting for events. Mastering shell scripting loops improves automation efficiency, reduces manual work, and is essential for real-world Linux and bash scripting tasks.

What Are Loops in Shell Scripting?

Loops in shell scripting are control structures that allow a set of commands to be executed repeatedly based on a condition or a list of values. Instead of writing the same command multiple times, you define a loop once and let the script handle repetition automatically. This makes your scripts:
  • More efficient
  • Easier to maintain
  • Cleaner and scalable
Loops are widely used in Linux shell programming, especially for automation tasks.

Types of Loops in Shell Scripting

There are three main types of loops in bash scripting loops tutorial:
  • for loop in shell scripting
  • while loop in shell scripting
  • until loop in shell scripting
Each loop serves a different purpose depending on the situation.

1. For Loop in Shell Scripting

The for loop in shell scripting is used when you already know the list of items or the number of iterations.

Syntax

for variable in list
do
    commands
done

Example: Printing Items

#!/bin/bash
for fruit in apple banana cherry
do
    echo "Fruit: $fruit"
done

Example: Number Iteration

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

Use Cases

  • Iterating over files in a directory
  • Running scripts on multiple servers
  • Processing predefined datasets
This is one of the most commonly used shell script for loop examples in automation.

2. While Loop in Shell Scripting

The while loop in shell scripting runs as long as a condition remains TRUE. It is ideal when you don’t know how many times the loop should execute.

Syntax

while [ condition ]
do
    commands
done

Example: Counter

#!/bin/bash
count=1
while [ $count -le 5 ]
do
    echo "Count: $count"
    count=$((count + 1))
done

Example: Read File Line by Line

while read line
do
    echo "$line"
done < file.txt

Use Cases

  • Monitoring system processes
  • Reading files line by line
  • Continuous condition checking
This is a powerful concept in shell scripting automation using loops.

3. Until Loop in Shell Scripting

The until loop in shell scripting runs until a condition becomes TRUE. It is essentially the opposite of a while loop.

Syntax

until [ condition ]
do
    commands
done

Example: Waiting for File

#!/bin/bash
until [ -f /tmp/ready.txt ]
do
    echo "Waiting for file..."
    sleep 2
done
echo "File found!"

Use Cases

  • Waiting for a service to start
  • Checking file availability
  • Handling delayed processes
This loop is useful in real-world shell scripting loops where you are waiting for events. Start Your Training Journey Today

Difference Between for, while, and until Loop

Loop Type Condition Best Use Case
for loop Iterates over list Known values or range
while loop Runs when condition is TRUE Unknown iterations
until loop Runs when condition is FALSE Waiting for condition
Understanding the difference between while and until loop in shell scripting is crucial for writing efficient scripts.

Loop Control in Shell Scripting (break and continue)

To gain better control over loops, shell scripting provides: break Exits the loop immediately. continue Skips the current iteration and moves to the next.

Example

for i in {1..10}
do
    if [ $i -eq 3 ]; then
        continue
    fi
    if [ $i -eq 7 ]; then
        break
    fi
    echo $i
done
This demonstrates break and continue in shell script, which is essential for controlling execution flow. Explore Courses - Learn More

Real-World Examples of Shell Scripting Loops

Here are some practical applications:

File Processing

Use for loop in shell scripting to iterate files:
for file in *.txt
do
    echo "Processing $file"
done

System Monitoring

Use while loop in shell scripting:
while true
do
    df -h
    sleep 10
done

Service Waiting

Use until loop in shell scripting:
until ping -c 1 google.com
do
    echo "Waiting for network..."
    sleep 5
done
These are excellent bash loop examples for automation.

Common Mistakes in Shell Scripting Loops

  • Infinite loops due to wrong conditions
  • Missing increment in while loops
  • Incorrect syntax spacing ([ ])
  • Not handling edge cases
Avoiding these improves your shell scripting best practices.

Conclusion

Mastering loops in shell scripting is essential for building efficient and scalable automation scripts. Use for loop when values are known Use while loop for condition-based execution Use until loop when waiting for something to happen When combined with real-world logic, loops become a powerful tool in bash scripting and Linux automation. The more you practice shell scripting loops examples, the more efficient your scripts will become — ultimately saving time and reducing manual effort. Talk to Academic Advisor

Frequently Asked Questions

Loops are control structures used to execute commands repeatedly based on conditions or lists.

A while loop runs when the condition is true, whereas an until loop runs until the condition becomes true.

The for loop in shell scripting is best for iterating over files.

Ensure correct conditions and always update variables inside loops.

Author

Embedded Systems trainer – IIES

Updated On: 09-04-26


10+ years of hands-on experience delivering practical training in Embedded Systems and it's design