Loops and positional arguments
Quick answer
Use loops to repeat a small task across files without copying the same command repeatedly. Positional parameters represent arguments given to a script.
Prepare the practice environment
Run python3 lab-fixtures/shell-scripting/setup.py as an ordinary user from the extracted package root in a disposable Linux environment. Enter the printed directory, check pwd, then run export LC_ALL=C; umask 077; set -o pipefail. Use a fresh fixture for each example. Save the code as example.sh inside that directory and run bash --noprofile --norc -o pipefail example.sh rather than sourcing it. Stop if a command fails unexpectedly. All output paths must be new. The source of check-report.sh is in the fixture JSON. Outputs below are not commands.
How it works
$1 is the first argument, $# is the count, and "$@" expands to the original arguments separately, including empty arguments. for item in "$@"; do ...; done preserves argument boundaries. while repeats while a command succeeds; read -r reads a line without treating backslashes as escapes. IFS= read -r also preserves surrounding whitespace. The simple read loop below assumes newline-terminated text. set -- sets positional arguments in the current shell for practice.
An empty argument still counts toward $#; an empty argument list has count zero. Quoted "$@" keeps these cases distinct. When reading records, the final line may have bytes but no newline; in Bash, read then assigns the bytes yet returns failure. The simple loop assumes terminated lines. To handle a nonempty unterminated final line deliberately, the condition can be IFS= read -r line || [ -n "$line" ]. Keep the same input redirection on the loop.
Try the examples
set -- 'daily report.txt' notes.txt ''printf 'count=%s\n' "$#"for item in "$@"; do printf '[%s]\n' "$item"doneSpaces and an empty argument remain intact.
Expected stdout:
count=3[daily report.txt][notes.txt][]for path in notes.txt 'daily report.txt' absent.txt; do if [ -f "$path" ]; then printf 'found: %s\n' "$path" fidoneThe conditional skips the missing file.
Expected stdout:
found: notes.txtfound: daily report.txtwhile IFS= read -r line; do printf '[%s]\n' "$line"done < notes.txtInput redirection connects the file to the loop.
Expected stdout:
[alpha][beta]Common mistakes
Never parse ls output to obtain filenames. Do not use unquoted $@; it can split arguments or expand wildcards.
Practice
Pass two filenames with spaces and one empty argument to a loop. Confirm that each is printed once, then try zero arguments.
Related references
Study: bash, printf. These are reference names, not deployed links.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Loops and positional arguments — Knowledge Check
Apply the lesson concepts to the controlled shell fixture.
What does "$@" preserve?
What does $# represent?
Why use read -r?
How many iterations for for item in "$@" with zero arguments?