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.
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.
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.