- Posted on
- • Advanced
Mastering Loops in Bash: `for`, `while`, and `until`
- Author
-
-
- User
- Linux Bash
- Posts by this author
- Posts by this author
-
Loops in Bash are essential for automating repetitive tasks, iterating through lists, or executing commands multiple times. Bash provides three primary types of loops: for, while, and until. Each has its own use cases and syntax.
1. for Loop
The for loop in Bash is used to iterate over a list of items (such as numbers, files, or strings) and execute a block of code for each item.
Syntax:
for variable in list
do
# Commands to execute
done
Example 1: Iterating Over a List of Items
for fruit in apple banana cherry
do
echo "I love $fruit"
done
Output:
I love apple
I love banana
I love cherry
Example 2: Iterating Over a Range of Numbers (using {})
for i in {1..5}
do
echo "Number $i"
done
Output:
Number 1
Number 2
Number 3
Number 4
Number 5
Example 3: Iterating with Step Size
You can specify a step size when iterating over a range using the seq command or a specific step in the {} range.
for i in {1..10..2}
do
echo "Odd number: $i"
done
Output:
Odd number: 1
Odd number: 3
Odd number: 5
Odd number: 7
Odd number: 9
Alternatively, using seq:
for i in $(seq 1 2 10)
do
echo "Odd number: $i"
done
2. while Loop
The while loop runs as long as a given condition is true. It is useful when you don't know how many times you need to iterate, but you have a condition to check before continuing the loop.
Syntax:
while condition
do
# Commands to execute
done
Example 1: Basic while Loop
count=1
while [ $count -le 5 ]
do
echo "Count is $count"
((count++)) # Increment count by 1
done
Output:
Count is 1
Count is 2
Count is 3
Count is 4
Count is 5
Example 2: Looping Until a Condition is Met
You can use a while loop to keep iterating as long as a condition is true (or until it's false).
count=5
while [ $count -gt 0 ]
do
echo "Count is $count"
((count--)) # Decrement count by 1
done
Output:
Count is 5
Count is 4
Count is 3
Count is 2
Count is 1
3. until Loop
The until loop works similarly to the while loop, but it continues as long as the condition is false. It’s used when you want to execute commands until a certain condition becomes true.
Syntax:
until condition
do
# Commands to execute
done
Example 1: Basic until Loop
count=1
until [ $count -gt 5 ]
do
echo "Count is $count"
((count++)) # Increment count by 1
done
Output:
Count is 1
Count is 2
Count is 3
Count is 4
Count is 5
Example 2: Infinite until Loop (with a break)
You can also create an infinite until loop. This is often used with a break statement to stop the loop when a certain condition is met.
count=1
until [ $count -gt 5 ]
do
echo "Count is $count"
((count++))
if [ $count -eq 3 ]; then
echo "Stopping at count 3"
break
fi
done
Output:
Count is 1
Count is 2
Count is 3
Stopping at count 3
4. Loop Control Statements
break: Exits the loop prematurely.continue: Skips the rest of the current iteration and moves to the next one.
Example with break:
for i in {1..5}
do
if [ $i -eq 3 ]; then
echo "Breaking at $i"
break
fi
echo "Number $i"
done
Output:
Number 1
Number 2
Breaking at 3
Example with continue:
for i in {1..5}
do
if [ $i -eq 3 ]; then
continue # Skip the rest of the loop for i=3
fi
echo "Number $i"
done
Output:
Number 1
Number 2
Number 4
Number 5
5. Nested Loops
You can nest loops within each other to perform more complex tasks.
Example: Nested for Loops
for i in {1..3}
do
for j in {1..2}
do
echo "i=$i, j=$j"
done
done
Output:
i=1, j=1
i=1, j=2
i=2, j=1
i=2, j=2
i=3, j=1
i=3, j=2
Example: Nested while Loop
i=1
while [ $i -le 3 ]
do
j=1
while [ $j -le 2 ]
do
echo "i=$i, j=$j"
((j++))
done
((i++))
done
Output:
i=1, j=1
i=1, j=2
i=2, j=1
i=2, j=2
i=3, j=1
i=3, j=2
Summary of Loops in Bash:
forloop: Iterates over a list of items (or range) and executes commands for each item.- Best for known iterations or ranges.
whileloop: Executes commands as long as the condition is true.- Useful when you want to repeat something until a condition changes.
untilloop: Executes commands until the condition becomes true.- Opposite of the
whileloop; it stops when the condition is true.
- Opposite of the
Loop control: Use
breakto exit early orcontinueto skip the current iteration.
By mastering these loops and their variations, you'll be able to automate a wide range of tasks in Bash effectively!
Further Reading
For further reading on Bash scripting loops, consider these resources:
Understanding Bash Loops: A structured guide on when and how to use
for,while, anduntilloops in Bash.Advanced Bash-Scripting Guide: Detailed explanations and examples of complex cases using loops in Bash.
forLoop Techniques: Explores various applications of theforloop in shell scripting.Practical Examples of
whileanduntilLoops: Real-world applications of while and until loops.Efficient Scripting with Loop Control: Tips on using
breakandcontinuestatements in loops.
Each of these links expands on the loop concepts covered in the initial guide, providing deeper insights and practical examples for mastering loops in Bash scripting.