Python Loops: for, while, until & Loop Control (break, continue)
Loops repeat code blocks across lists of files, arrays, integer ranges, or streaming inputs. Bash offers standard `for in` loops, C-style three-expression `for (( ; ; ))` loops, condition-driven `while` loops, and inverted `until` loops.
"Loops are the automated conveyor belts of scripting: instead of manually processing 100 files one by one, you load them onto the loop belt and apply the same operation to every item."
Deep Dive: How It Works
for in Loops: `for file in *.log; do ... done` (note: always quote `"$file"` inside body).
Range Expansion: `for i in {1..5}; do ... done` or `for i in {0..100..10}; do ... done` (with step size 10).
C-Style for Loops: `for (( i=1; i<=10; i++ )); do ... done`.
Loop Control: `break` exits the loop immediately; `continue` skips the rest of the current iteration.
Syntax Blueprint
for item in "${ARRAY[@]}"; do
echo "$item"
done
for (( i=0; i<5; i++ )); do
echo "Index: $i"
done
while [[ $COUNT -gt 0 ]]; do
(( COUNT-- ))
doneUse for in for collections and while read for streaming lines.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Using `for file in $(ls *.txt)` to iterate over files.Why it happens: Command substitution splits filenames containing spaces into multiple broken loop iterations.
How to fix: Use direct globbing instead: `for file in *.txt; do [[ -e "$file" ]] || continue; ... done`.
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Iterate Through a Range with for
Write a for loop `for num in {1..3}; do echo "Item $num"; done`.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.