Bash Loops in Bash
A loop runs a block of code multiple times without writing it repeatedly. Instead of typing the same command 100 times, a loop handles it in a few lines. Bash provides three main types of loops: for, while, and until.
for Loop
A for loop iterates over a list of values. Each item in the list takes a turn being assigned to the loop variable, and the code block runs once per item.
Syntax
for variable in list do # commands done
Example – Loop Over a List of Words
#!/bin/bash for fruit in Apple Banana Mango Orange do echo "Fruit: $fruit" done
Output:
Fruit: Apple Fruit: Banana Fruit: Mango Fruit: Orange
Example – Loop Over a Number Range
#!/bin/bash
for num in {1..5}
do
echo "Number: $num"
done
Output:
Number: 1 Number: 2 Number: 3 Number: 4 Number: 5
for Loop with Step Value
#!/bin/bash
for num in {2..10..2}
do
echo $num
done
Output:
2 4 6 8 10
C-style for Loop
This style works exactly like a for loop in C or Java.
#!/bin/bash for (( i=1; i<=5; i++ )) do echo "Count: $i" done
Output:
Count: 1 Count: 2 Count: 3 Count: 4 Count: 5
while Loop
A while loop keeps running as long as a condition remains true. It checks the condition before every iteration.
Syntax
while [ condition ] do # commands done
Example – Countdown Timer
#!/bin/bash count=5 while [ $count -gt 0 ] do echo "Countdown: $count" ((count--)) done echo "Launch!"
Output:
Countdown: 5 Countdown: 4 Countdown: 3 Countdown: 2 Countdown: 1 Launch!
until Loop
An until loop is the opposite of while. It runs as long as the condition is false. It stops when the condition becomes true.
Syntax
until [ condition ] do # commands done
Example
#!/bin/bash num=1 until [ $num -gt 5 ] do echo "Number: $num" ((num++)) done
Output:
Number: 1 Number: 2 Number: 3 Number: 4 Number: 5
Loop Type Comparison
┌──────────────────────────────────────────────────────┐ │ Loop Type Summary │ │ │ │ for → Use when the number of iterations is known │ │ e.g., loop 5 times, loop over a list │ │ │ │ while → Use when iterations depend on a condition │ │ e.g., keep running until user types quit │ │ │ │ until → Use when stopping on a TRUE condition │ │ e.g., wait until a file appears │ └──────────────────────────────────────────────────────┘
Loop Control – break and continue
break – Exit the Loop Early
The break statement immediately exits the loop, even if the condition is still true.
#!/bin/bash
for num in {1..10}
do
if [ $num -eq 6 ]; then
echo "Stopping at $num"
break
fi
echo "Number: $num"
done
Output:
Number: 1 Number: 2 Number: 3 Number: 4 Number: 5 Stopping at 6
continue – Skip the Current Iteration
The continue statement skips the rest of the current iteration and moves to the next one.
#!/bin/bash
for num in {1..6}
do
if [ $num -eq 3 ]; then
echo "Skipping $num"
continue
fi
echo "Number: $num"
done
Output:
Number: 1 Number: 2 Skipping 3 Number: 4 Number: 5 Number: 6
Looping Over Files
#!/bin/bash for file in /home/john/documents/*.txt do echo "Found file: $file" done
Infinite Loop with while true
An infinite loop runs forever until a break or Ctrl+C stops it. This pattern is common in scripts that wait for user input or monitor a process.
#!/bin/bash
while true
do
read -p "Type 'quit' to exit: " input
if [ "$input" = "quit" ]; then
echo "Goodbye!"
break
fi
echo "You typed: $input"
done
Nested Loops – Loop Inside a Loop
#!/bin/bash
for row in 1 2 3
do
for col in A B C
do
echo "Row $row, Col $col"
done
done
Output:
Row 1, Col A Row 1, Col B Row 1, Col C Row 2, Col A Row 2, Col B Row 2, Col C Row 3, Col A Row 3, Col B Row 3, Col C
Practical Example – Multiplication Table
#!/bin/bash
read -p "Enter a number: " n
for i in {1..10}
do
printf "%d x %2d = %d\n" $n $i $((n * i))
done
Output (for n=5):
5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 ... 5 x 10 = 50
Key Takeaways
- Use
forto loop over a list or a known number of times. - Use
whileto loop as long as a condition is true. - Use
untilto loop until a condition becomes true. - Use
breakto exit a loop andcontinueto skip the current iteration. - The
while truepattern creates an infinite loop that must be stopped withbreakorCtrl+C.
