πŸ”„ 3. Bash Control Flow
Estimated reading: 3 minutes 40 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

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

Or Copy Link

CONTENTS
Scroll to Top