Skip to content

Chapter 4 of 8

Loops and Iteration

Bash offers several ways to iterate. The most basic form is for name in list; do body; done, where the list may be a literal set of words, a glob, the output of a command substitution, or an array expansion. Because unquoted globs and command substitutions are subject to word splitting, list iteration over arrays should always be written for item in "${arr[@]}", with both the subscript and the variable quoted, so that elements containing spaces or special characters are preserved correctly. A more verbose variant, the C-style for loop — for (( i=0; i<10; i++ )) — uses arithmetic expression syntax and is convenient for counting loops and parallel iteration.

Conditional loops take the form while condition; do body; done and until condition; do body; done. The condition is re-evaluated after each iteration; a while loop continues as long as the command returns zero, and an until loop continues as long as it returns non-zero — an until is essentially a negated while, handy for retry patterns like until ping -c1 host; do sleep 5; done. Inside any loop, break exits the innermost loop and continue skips to the next iteration; both accept an optional integer level, so break 2 exits two nested loops at once.

Reading a file line by line is a special case of a while loop: while IFS= read -r line; do echo "$line"; done < file.txt. Setting IFS= to an empty string preserves leading and trailing whitespace on each line, and the -r flag prevents read from interpreting backslash escapes. To read fields separated by a delimiter into separate variables, supply a custom IFS: IFS=: read -r user _ uid _ _ home _ < /etc/passwd, where the placeholder _ simply discards fields you do not care about. The same pattern adapted to "\(@"for arg in "\)@"; do echo "$arg"; done — is the canonical way to iterate over a script's own command-line arguments.

All chapters
  1. 1Foundations of Bash Scripting
  2. 2Variables, Expansion, Quoting, and Strict Mode
  3. 3Conditionals and Tests
  4. 4Loops and Iteration
  5. 5Functions, Sourcing, and Command-Line Arguments
  6. 6Arrays, Strings, Substitutions, and Arithmetic
  7. 7Input/Output, Redirection, and File Handling
  8. 8Process Control, Scheduling, and Robust Practices

Drill it

Reading is not remembering. These come from the Bash Scripting For Automation deck:

Q

What is Bash?

Bash (Bourne Again SHell) is a Unix shell and command language written as a free software replacement for the Bourne shell (sh). It is the default login shell o...

Q

What file extension is conventionally used for Bash scripts?

.sh. While not strictly required for execution, .sh signals intent and lets tooling treat the file as a shell script.

Q

What must the first line of an executable Bash script be?

A shebang line, e.g. #!/usr/bin/env bash or #!/bin/bash, which tells the kernel which interpreter to invoke.

Q

What command makes a script file executable?

chmod +x script.sh (or chmod 755 script.sh).