|
|
|
The |
|
|
Basic for loop over a list of words: |
|
|
Loop over files in a directory using globbing: |
|
|
Loop over command output using command substitution: |
|
|
POSIX sh lacks range syntax for ‘for’, so use ‘while’ instead: |
|
|
Bash
Bash provides a C-style for loop with cleaner numeric iteration: |
|
|
Bash also supports brace expansion for ranges: |
|
|
Step values work too: {1..10..2} gives 1,3,5,7,9 |
|
|
You can also use seq if available: |
|
|
Loop over positional parameters: |
|
|
Loop over lines in a file (safer than for loop): |
|
|
The loop variable persists after the loop ends. |
|