πŸ”„ 3. Bash Control Flow
Estimated reading: 3 minutes 349 views

🐚 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, and until loops
  • 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

TipBenefit
Quote variables in loopsPrevents word splitting
Use [[ ]] for conditionsSafer and more flexible
Avoid infinite loops without breakPrevent resource exhaustion
Use for (( )) for numeric loopsCleaner 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 for to iterate over items or ranges
  • Use while to repeat while a condition is true
  • Use until to repeat until a condition becomes true
  • Use break to exit loops and continue to 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 :
Share

🟒 Bash: Looping Constructs (for, while, until)

Or Copy Link

CONTENTS
Scroll to Top