<aside> 🔁
Module 6 — Loops. Doing something to 50 files or 500 hosts without typing it 500 times — the core of every batch operation and fleet task.
🧠 concept → 🧩 analogy → 🧪 exercise → ✅ expected result (hidden) → 🎯 interview questions (hidden)
</aside>
<aside> 📋
Before you start. You need Modules 1–5. Load-bearing here: word splitting and globbing (Module 2 — one famous loop trap is built from them), exit codes (Module 3 — while runs on verdicts exactly as if does), < redirection (Module 4), and [ ]/case (Module 5). Tools: nothing new. Keep ~/bash-course.
</aside>
<aside> 📖
Official docs: Bash manual — Looping Constructs
</aside>
The for loop runs its body once per word in a list, with a variable holding the current word each time:
for name in alice bob carol; do
echo "Provisioning account for $name"
done
Read it as a sentence: for each name in this list, do the body, done. The pieces: name is an ordinary variable (Module 2 rules apply — read it as "$name" inside the body); the list is any sequence of words; do…done bracket the body the way then…fi bracketed a conditional. And the crucial mechanical fact, on which both the power and the traps of this module hang: the list is built by the shell's ordinary rewriting machinery first — expansion, splitting, globbing (Module 2's pipeline) — and then the loop runs over whatever words emerged.
<aside> 🧩
Real-world analogy — the clipboard round. A ward nurse on medication rounds: a clipboard lists patients; the nurse visits each in turn, performing the same procedure with a different name filled in each time. The clipboard is written out completely before the round starts — patients admitted mid-round are not on it.
Where the analogy stops working. The nurse reads names as lines on a form. The shell's clipboard is built by word splitting — one name containing a space becomes two visits to two nonexistent patients. Every for-loop trap in this module is that one mismatch wearing different costumes, so keep the clipboard image and this correction together.
</aside>
🧪 Exercise 6.1 — the round
for name in alice bob carol; do
echo "Provisioning account for $name"
done
<aside> 📖
Official docs: Bash manual — Looping Constructs · Bash manual — Shell Expansions (brace expansion)
</aside>
"Do this N times" is just a for-in over a list of numbers, and Module 1's brace expansion builds the list: for i in {1..5} — bash rewrites {1..5} to 1 2 3 4 5 before the loop ever looks. For loops whose count is computed rather than fixed, bash borrows C's syntax: for ((i=1; i<=3; i++)) — start, keep-going condition, step. Inside (( )) you are in arithmetic land: a small dialect where variables need no $, <= compares numbers (not strings — no Module 5 dictionary surprises), and i++ means add one. The same land is reachable inline anywhere as $(( expression )) — arithmetic expansion, the missing tool for counters: n=$((n-1)) computes n minus one and assigns it back. (Bash arithmetic is integer-only; $((7/2)) is 3. Decimals need other tools — Module 11's awk.)
🧪 Exercise 6.2 — three ways to count
for i in {1..5}; do echo "attempt $i"; done
for ((i=1; i<=3; i++)); do echo "tick $i"; done
n=10
echo "$((n / 3)) and a remainder of $((n % 3))" # integer division; % = remainder