π Bash Looping Constructs β for, while, and until Loops with Examples
Introduction to Bash Loops β Automate Repetitive Tasks with for, while, and until
Loops are essential in Bash scripting for automating repetitive tasks such as iterating over files, performing countdowns, or processing input. Bash provides three main looping constructsβfor, while, and untilβeach with its own use case depending on the condition or iteration pattern.
Mastering loops allows you to write dynamic and efficient shell scripts that can handle a wide range of automation scenarios.
In this article, youβll learn:
- How to write Bash
for,while, anduntilloops - Use cases for each type of loop
- Syntax differences and best practices
- Real-world looping examples
Bash for Loop β Iterating Over a List
The for loop is used to iterate over a sequence, such as file names, numbers, or arguments.
Syntax:
for variable in list; do
# commands
done
Example 1: Looping Through Strings
for fruit in apple banana cherry; do
echo "Fruit: $fruit"
done
Output:
Fruit: apple
Fruit: banana
Fruit: cherry
Example 2: Looping Through Files
for file in *.txt; do
echo "Processing $file"
done
Example 3: C-style for Loop
for ((i=1; i<=5; i++)); do
echo "Count: $i"
done
Output:
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
Bash while Loop β Run While a Condition Is True
The while loop runs as long as the condition remains true.
Syntax:
while condition; do
# commands
done
Example: Countdown
count=5
while [ $count -gt 0 ]; do
echo "Countdown: $count"
((count--))
done
Output:
Countdown: 5
Countdown: 4
Countdown: 3
Countdown: 2
Countdown: 1
Bash until Loop β Run Until Condition Is True
The until loop is the opposite of while. It continues until the condition becomes true (i.e., while it’s false).
Syntax:
until condition; do
# commands
done
Example: Count Up to 5
count=1
until [ $count -gt 5 ]; do
echo "Value: $count"
((count++))
done
Output:
Value: 1
Value: 2
Value: 3
Value: 4
Value: 5
βΉοΈ Breaking or Skipping in Loops
break β Exit the Loop
for i in {1..5}; do
if [ $i -eq 3 ]; then
break
fi
echo "i = $i"
done
continue β Skip Current Iteration
for i in {1..5}; do
if [ $i -eq 3 ]; then
continue
fi
echo "i = $i"
done
Best Practices for Bash Loops
| Tip | Benefit |
|---|---|
| Quote variables in loops | Prevents word splitting |
Use [[ ]] for conditions | Safer and more flexible |
Avoid infinite loops without break | Prevent resource exhaustion |
Use for (( )) for numeric loops | Cleaner syntax for counting |
Summary β Bash Loops
Loops are a key component of any Bash script. Whether you’re iterating over arguments, reading input, or automating tasks across files, for, while, and until loops offer the tools you need.
Key Takeaways:
- Use
forto iterate over items or ranges - Use
whileto repeat while a condition is true - Use
untilto repeat until a condition becomes true - Use
breakto exit loops andcontinueto skip iterations
Real-world Uses:
- Loop through backup files
- Read lines from a config file
- Retry network checks with a timeout
FAQ β Bash Loops
What is the difference between for and while in Bash?
for loops iterate over a predefined list or range, while while loops continue as long as a condition is true.
How do I loop through a range of numbers in Bash?
Use C-style loop:
for ((i=1; i<=5; i++)); do echo $i; done
Whatβs the difference between while and until?
while loops run while the condition is true. until loops run until the condition becomes true (i.e., while it’s false).
How do I stop a loop early in Bash?
Use break inside the loop:
if [[ $x -eq 5 ]]; then break; fi
Can I loop over lines in a file?
Yes:
while IFS= read -r line; do
echo "$line"
done < filename.txt
Share Now :
