Q1What does for f in *.txt; do ... done loop over?
Loops — Processing Files One by One with for
This article is part of the Linux Course, where you master practical Linux skills from scratch, from basic commands through to shell scripting.
Loop over a file list with for f in *.txt, repeat conditionally with while [ "$i" -le 3 ] and until, skip blank values with continue, and stop at a limit with break — illustrated and practiced in a browser terminal.
Process a list in order — for
for takes the values you give it one at a time and runs the same work on each.
The form is for v in a b c; do ... done, where v becomes a, then b, then c in turn.
What sits between do and done is the body that repeats.
You can list the values directly, use a file pattern like *.txt, or feed in the output of $(...) from a command.
Write for f in *.txt and each .txt file in the current directory goes into f in turn, so you can write per-file work.
Using $(...) as in for line in $(cat list.txt) lets you loop over the output of a command.
This is how you write summaries of a file list or batch processing across several files.
for env in dev stg prod; do # process the listed values in turn
echo "deploying to $env"
done
for f in *.sh; do # process the .sh files in turn
echo "script: $f"
done
for pulls items from the list into f one at a time and runs the body, then finishes at done once the items run out.| Syntax | Meaning | Example |
|---|---|---|
for v in a b c; do … done | process the listed values in turn | for v in dev stg prod; do echo $v; done |
for f in *.txt; do … | process matching files in turn | for f in *.txt; do wc -l "$f"; done |
for x in $(cmd); do … | process command output in turn | for d in $(ls); do echo $d; done |
do … done | start and end of the loop body | for v in a b; do echo $v; done |
Loop on a condition — while / until
while repeats as long as the condition is true.
As in while [ "$i" -lt 3 ]; do ... done, you increment a counter inside the body to head toward the end.
until is the opposite — it repeats until the condition becomes true.
It suits work where the number of rounds is not fixed in advance.
i=1
while [ "$i" -le 3 ]; do # repeat while i is 3 or less
echo "try $i"
i=$((i + 1))
done # try 1 / try 2 / try 3
i=1
until [ "$i" -gt 3 ]; do # repeat until the condition becomes true
echo "i=$i"
i=$((i + 1))
done # i=1 / i=2 / i=3
while repeats the body while the condition is true; until repeats it until the condition becomes true. Both advance a counter in the body to head toward the end.| Syntax | Meaning | Example |
|---|---|---|
while [ cond ]; do … done | repeat while the condition is true | while [ "$i" -lt 3 ]; do …; done |
until [ cond ]; do … done | repeat until the condition becomes true | until [ -f done.flag ]; do …; done |
Control a for loop — break / continue
Even partway through a for loop, you can change the flow with break and continue.
continue skips the rest of that round and moves on to the next item, while break ends the loop itself.
Combine them with a condition as in if [ condition ]; then continue; fi or if [ condition ]; then break; fi to skip items you don't need or stop once you have enough.
for n in 1 2 3 4 5; do
if [ "$n" -eq 3 ]; then continue; fi # skip 3
if [ "$n" -eq 5 ]; then break; fi # break out at 5
echo "n=$n"
done # n=1 / n=2 / n=4
for runs the body in order from n=1. At n=3 the continue skips the rest and moves on, and at n=5 the break ends the loop itself (output is n=1 / n=2 / n=4).| Syntax | Meaning | Example |
|---|---|---|
continue | skip this round and go to the next | if [ -z "$x" ]; then continue; fi |
break | exit the loop partway through | if [ "$n" -gt 100 ]; then break; fi |
Control a while loop — break / continue
On top of the condition check in while, using break and continue lets you stop early or skip a round.
continue skips that round and goes back to the condition check, while break exits the loop without waiting on the condition.
Watch out here: if you don't place the counter update at the top of the body (before continue), the skipped round won't update it and the loop never ends.
i=0
while [ "$i" -lt 10 ]; do
i=$((i + 1)) # advance at the top
if [ "$i" -eq 3 ]; then continue; fi # skip the 3rd round
if [ "$i" -eq 6 ]; then break; fi # break out on the 6th round
echo "attempt $i"
done # attempt 1 / 2 / 4 / 5
while runs the body while the condition is true, advancing i by 1 each time. At i=3 the continue goes back to the condition check, and at i=6 the break ends the loop without waiting on the condition (output is attempt 1 / 2 / 4 / 5).| Syntax | Meaning | Example |
|---|---|---|
continue | skip this round, back to the check | if [ $((i % 3)) -eq 0 ]; then continue; fi |
break | exit the loop without waiting on the condition | if [ "$found" -ge 4 ]; then break; fi |
| where to update the counter | update it before continue | put i=$((i + 1)) at the top of the body |
Knowledge Check
Answer each question one by one.
Q2Which keyword repeats while the condition is true?
Q3Which one skips just that round and moves on to the next?