π 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 :
