Linux › Section K · Shell scripts › Topic K.4
Loops and command substitution
Loops let a script do the same job for every file, argument or line, and command substitution lets it capture a command's output and use it as data. Together they turn a one-off command into something that handles a whole directory.
Repeating work
The Bourne shell has two main loops. A for loop runs once for each word in a list, which suits files and arguments. A while loop runs as long as a command keeps succeeding, using exit codes just as if does in "Conditionals". Both wrap their body in the keywords do and done. Command substitution, the second half of this topic, captures a command's output so it can become a list for a loop, a variable or an argument. All outputs below come from real runs in bash 5.2.
for loops
for word in one two three; do echo "item: $word"; done prints item: one, item: two and item: three. The shell sets the variable to the first word after in, runs the body, then moves on to the next word until the list is used up. The list can be a glob: in a directory holding a.log and b.log, for f in *.log; do echo "found $f"; done prints found a.log and found b.log. A filename containing a space stays one item, because glob results are not split again; inside the body, quote "$f" so that it stays whole there too. Leaving out in list makes for loop over the script's arguments, exactly like for arg in "$@".
When a glob matches nothing
If no file matches, the shell leaves the pattern as it is. With no CSV files present, for f in *.csv; do echo "csv: $f"; done prints csv: *.csv, one pass with a nonsense name. The usual guard is a test at the top of the body: [ -e "$f" ] || continue. Now nothing is printed and the script carries on. continue skips the rest of the current pass and starts the next one; break leaves the loop entirely. In for i in 1 2 3 4 5 6 with continue when i is 2 and break when i is 5, the output is i=1, i=3 and i=4.
while and until
while runs a command, executes the body if it succeeded, and repeats; it stops at the first non-zero status. A counter needs shell arithmetic, $(( )), which "Important shell script utilities" covers in full: n=1; while [ $n -le 3 ]; do echo "n=$n"; n=$((n + 1)); done prints n=1, n=2 and n=3. until is the mirror image: it repeats while the test fails and stops at the first success. Any command can serve as the test, such as grep -q or ping, not just [. Forget to change the condition and the loop never ends; Ctrl+C stops it. bash also accepts a C-style counting loop, for ((i = 0; i < 3; i++)), which prints 0, 1 and 2 but does not work in dash.
Reading a file line by line
A while loop combined with read processes input one line at a time. Given people.txt with the lines alice 30, bob 25 and carol 41 (two leading spaces), while read -r name age; do echo "$name is $age"; done < people.txt prints alice is 30, bob is 25 and carol is 41. read splits each line into fields at whitespace and gives the last named variable whatever is left. The -r option stops backslashes being treated as escapes. To keep each line exactly, leading spaces included, write while IFS= read -r line, which prints [ carol 41] when each line is shown in brackets. The redirection < people.txt after done feeds the whole loop.
Worked example: waiting with a limit
A script must wait until a file ready.flag appears, but give up after five checks. 1. Start a counter: tries=0. 2. Loop with until [ -e ready.flag ]; do, so the body runs while the file is missing. 3. In the body, add one with tries=$((tries + 1)); if [ $tries -gt 5 ], print gave up to standard error and exit 1. 4. Otherwise print waiting ($tries), run sleep 1 and close with done. After the loop, print ready after $tries checks. 5. Test both paths. With another process creating the file about 1.5 seconds in, one run printed waiting (1), waiting (2), then ready after 2 checks, status 0. With no file, it printed waiting (1) to waiting (5), then gave up, status 1. The limit guarantees the loop ends.
Command substitution
$(command) runs the command and puts its standard output on the command line in its place. today=$(date +%Y-%m-%d) followed by echo "Report for $today" printed Report for 2026-11-06 on the day of the run. n=$(wc -l < people.txt) stores the bare number 3; with < the file is read from standard input, so wc does not print its name. Substitutions nest without fuss: echo "$(basename "$(pwd)")" prints the current directory's last component. The shell removes all trailing newlines from the captured output, so printf 'a\nb\n\n\n' captures as just the two lines a and b. Older scripts use backticks, `date`; $( ) is the POSIX form and much easier to read and nest.
Quoting a substitution
An unquoted $( ) undergoes word splitting and globbing like a variable. for w in $(echo "one two"); do echo "w=$w"; done runs twice, printing w=one and w=two, but with "$(echo "one two")" it runs once and prints w=one two. That difference breaks a common shortcut. In a directory holding my notes.txt and people.txt, for f in $(ls *.txt) gives three items, my, notes.txt and people.txt, while for f in *.txt gives the correct two. Never loop over $(ls): the glob is faster, and it keeps names with spaces whole. For long lists of names from find, the pipeline to xargs in "Important shell script utilities" is the safer tool.
Worked example: counting lines in every text file
Report the line count of each .txt file and a grand total. 1. total=0 before the loop. 2. for f in *.txt; do, with [ -f "$f" ] || continue first, which skips directories and the unmatched-glob case. 3. lines=$(wc -l < "$f") captures just the number, and quoting "$f" protects my notes.txt. 4. Print echo "$f: $lines lines" and add total=$((total + lines)), then done and echo "Total: $total". 5. Run it on b.txt (1 line), my notes.txt (2 lines) and people.txt (3 lines). It printed b.txt: 1 lines, my notes.txt: 2 lines, people.txt: 3 lines and Total: 6. Check: 1 + 2 + 3 = 6, and the file with a space was counted once.
Common mistakes to avoid
First, forgetting do or done, or putting do on the same line without a semicolon before it. Second, looping over $(ls) or other unquoted output, which splits names with spaces. Third, not guarding a glob that may match nothing. Fourth, a while condition that never changes, so the loop never ends. Fifth, read without -r, or without IFS= when leading spaces matter. Sixth, piping into while read and expecting variables set in the loop to survive; the pipe runs the loop in a subshell, as "Subshells, including files, and reading input" explains.
Key takeaways
for name in list; do ...; done runs once per word, and a glob list keeps filenames with spaces intact; with no list it loops over the arguments. Guard unmatched globs with [ -e "$f" ] || continue. while repeats while its command succeeds and until while it fails; break leaves a loop and continue skips to the next pass. while IFS= read -r line; do ...; done < file reads lines exactly. $(command) substitutes a command's output, minus trailing newlines; quote it unless you want word splitting, and never loop over $(ls).
Flashcards
Flashcard
Runs the body once for each word, with x set to it
Test your understanding
Question 1 of 5
A directory contains no .csv files. What does for f in *.csv; do echo "csv: $f"; done print in bash?
Question 2 of 5
In a directory holding the files my notes.txt and people.txt, how many times does the body of for f in $(ls *.txt) run?
Question 3 of 5
What does n=1; while [ $n -le 3 ]; do echo $n; n=$((n + 1)); done print?
Question 4 of 5
Which loop reads a file one line at a time while keeping leading spaces and backslashes exactly?
Question 5 of 5
A command prints the lines a and b followed by two blank lines. What does x=$(command) store?
