|
The for VARIABLE in LIST; do commands using $VARIABLE done The loop assigns each item from LIST to VARIABLE, then
executes the commands between |
|
|
Output:
Iterating over fruits: I like apple I like banana I like cherry |
|
|
Loops eliminate repetitive code and let you process collections of items. Instead of writing the same command multiple times with different values, you write it once and let the loop handle each item. The loop approach is easier to modify, extend, and understand when dealing with many items. |
|
|
Output:
Without a loop (repetitive): processing: file1 processing: file2 processing: file3 With a loop (scalable): processing: file1 processing: file2 processing: file3 |
|
|
The Syntax forms:
The output of |
|
|
Output:
Count 1 to 5: 1 2 3 4 5 Count 3 to 7: 3 4 5 6 7 Count by 2s (1 to 9): 1 3 5 7 9 |
|
|
Bash
Bash provides two convenient syntaxes for numeric
iteration that don’t require external commands like Brace expansion C-style for loops |
|
|
Output:
Brace expansion {1..5}:
1
2
3
4
5
Brace expansion with step {1..10..2}:
1
3
5
7
9
C-style for loop:
1
2
3
4
5
C-style counting down:
3
2
1
|
|
|
Glob patterns (like Common glob patterns:
Important: if no files match the pattern, the glob
expands to the literal pattern string. The defensive
check |
|
|
Output:
Simple file iteration: found: /tmp/file1.txt found: /tmp/file2.txt found: /tmp/file3.txt Using a glob pattern: found: /tmp/file1.txt found: /tmp/file2.txt found: /tmp/file3.txt Defensive pattern (handles no matches): (no files matched, loop body never ran) |
|
|
Command substitution This pattern is useful for processing dynamic data like usernames, package lists, or filtered results. Caution: word splitting occurs on spaces, tabs, and
newlines. If your data contains spaces, consider using
|
|
|
Output:
First 5 users from /etc/passwd: - root - bin - daemon - lp - sync Simple word list from echo: word: one word: two word: three |
|
|
Use The Shorthand: |
|
|
Output:
Looping over "$@": arg: first arg arg: second arg arg: third arg Shorthand (omit 'in "$@"'): arg: first arg arg: second arg arg: third arg |
|
|
Bash
Bash arrays store multiple values in a single variable.
Use This is similar to Note: Arrays are a Bash feature and are not available in POSIX sh. |
|
|
Output:
Iterating over array elements: - apple - banana - cherry pie Array indices are also available: index 0: apple index 1: banana index 2: cherry pie |
|
|
The
These are useful for stopping early when you find what you need (break) or skipping items that don’t apply (continue). |
|
|
Output:
Using 'break' to exit early: checking 1 checking 2 found 3, stopping Using 'continue' to skip items: processing 1 processing 2 skipping 3 processing 4 processing 5 |
|
|
The loop variable persists after the loop ends, retaining the value from the last iteration. This differs from many programming languages where loop variables are scoped to the loop body. This behavior can be useful when you need to know the last processed item, but be careful to avoid accidentally reusing a variable name from a previous loop. |
|
|
Output:
After loop, letter = c Practical use - find last matching item: Last existing file: /etc/fstab |
|