7.2. Loops: for, while, and until#

Repeat code based on conditions or over collections of items.

7.2.1. Loop Comparison#

Loop Type

Best For

Example

for var in list

Fixed list/array

for file in *.txt

for (( i; cond; update ))

Counter-based

for (( i=0; i<10; i++ ))

while [ cond ]

Condition-based

while [ $count -lt 10 ]

until [ cond ]

Condition-based (opposite)

until [ $connected = yes ]

Quick decision guide:

  • Know how many iterations? → Use for

  • Iterate over items? → Use for var in list

  • Need counter/index? → Use C-style for

  • Loop until something happens? → Use while or until

7.2.1.1. Syntax: until [ condition ]#

until [ condition ]; do
  # code runs until condition becomes true
done

Comparison:

  • while [ $x -lt 10 ] - loop while x < 10 (stop when x >= 10)

  • until [ $x -ge 10 ] - loop until x >= 10 (same effect!)

Real example: retry logic

#!/bin/bash

until ping -c 1 8.8.8.8 &>/dev/null; do
  echo "Waiting for internet..."
  sleep 2
done

echo "Connected!"

Real example: wait for file

#!/bin/bash

until [ -f "/var/run/ready" ]; do
  echo "Waiting for service..."
  sleep 1
done

echo "Service is ready"

7.2.2. until Loop: Inverse while#

Execute until condition becomes true (loop while false):

7.2.2.1. Syntax: while [ condition ]#

while [ condition ]; do
  # code runs while condition is true
  # must eventually change condition!
done

Real example: count up

#!/bin/bash
i=1

while [ $i -le 5 ]; do
  echo "Number: $i"
  ((i++))
done

Real example: read lines from file

#!/bin/bash

while IFS= read -r line; do
  echo "Read: $line"
done < /etc/hosts

Real example: process user input

#!/bin/bash

while [ -z "$password" ]; do
  read -p "Enter password: " password
  [ -z "$password" ] && echo "Cannot be empty!"
done

echo "Password accepted"

Warning: Make sure you eventually change the condition, or loop forever!

# Bad: infinite loop
i=1
while [ true ]; do
  echo $i  # i never changes!
done

7.2.3. while Loop: Condition-Based#

Execute as long as condition is true:

7.2.3.1. Syntax: for (( init; condition; update ))#

for (( i=1; i<=5; i++ )); do
  echo "Number: $i"
done

Components:

  • i=1 - initialization

  • i<=5 - loop condition (keep looping while true)

  • i++ - update after each iteration

Real example: iterate with index

#!/bin/bash

for (( i=0; i<${#files[@]}; i++ )); do
  echo "$i: ${files[i]}"
done

Real example: countdown

#!/bin/bash

for (( i=10; i>=1; i-- )); do
  echo "$i..."
  sleep 1
done

echo "Blastoff!"

Real example: step by 2

for (( i=0; i<=20; i+=2 )); do
  echo $i
done

7.2.4. for Loop: C-Style (Counter)#

Loop with a numeric counter, like in C:

7.2.4.1. Syntax: for var in list#

for item in item1 item2 item3; do
  echo "Processing: $item"
done

Real example: process files

#!/bin/bash

for file in *.txt; do
  echo "Processing $file"
  wc -l "$file"
done

Real example: process command output

#!/bin/bash

for line in $(cat /etc/hostname); do
  echo "Line: $line"
done

Real example: hardcoded list

for color in red green blue; do
  echo "Color: $color"
done

7.2.5. for Loop: Iterate Over List#

Process each item in a sequence: