ADocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
for loop in Bash is a control flow construct used to iterate over a sequence of elements or to execute a block of code a specified number of times. Bash supports two distinct syntactic forms for the for loop: the standard POSIX list-iteration loop and the C-style arithmetic loop.
Standard List-Iteration Loop
The list-iteration loop executes a block of commands for each member of a provided list.- Shell Expansions: Before the loop initiates, the shell evaluates the
word_listthrough a strict sequence of expansions to generate the final list of items:- Initial Expansions: The shell first performs brace expansion, followed by tilde expansion, parameter and variable expansion, command substitution, and arithmetic expansion.
- Word Splitting: The shell scans the results of the expansions. It applies word splitting based on the Internal Field Separator (
$IFS) only to the unquoted results of parameter expansion, command substitution, and arithmetic expansion. It does not split the results of brace expansion, tilde expansion, or literal strings. - Pathname Expansion: After word splitting, the shell performs pathname expansion (globbing) on the resulting words to resolve file patterns (e.g.,
*,?). - Quote Removal: As the final step, all unquoted backslashes, single quotes, and double quotes that did not result from the previous expansions are removed.
- Assignment and Execution: For each resulting word from the final expanded list, the shell assigns the word’s value to
variable_nameand executes the commands enclosed between thedoanddonekeywords. - Implicit Iteration: If the
in word_listclause is omitted entirely (for variable_name; do), the loop implicitly iterates over the positional parameters ("$@"), which represent the arguments passed to the script or function.
C-Style Arithmetic Loop
The C-style loop utilizes Bash’s arithmetic evaluation compound command((...)) to control iteration through mathematical expressions.
- Initialization (
expr1): This arithmetic expression is evaluated exactly once before the loop begins. It is typically used to initialize a loop counter variable. - Condition (
expr2): This expression is evaluated before each iteration. It acts as the loop’s continuation condition. If the arithmetic evaluation results in a non-zero value (representing boolean true in C-style logic), the loop executes. If it evaluates to zero (boolean false), the loop terminates. Ifexpr2is omitted, it evaluates to true. - Step (
expr3): This expression is evaluated at the end of each iteration, immediately after the commands within thedo…doneblock have executed. It is typically used to increment or decrement the loop counter.
Loop Control Statements
The execution flow within both types offor loops can be modified using built-in control statements:
break [n]: Immediately terminates the loop. Ifnis specified, it breaks out ofnnested loops.continue [n]: Skips the remaining commands in the current iteration. In a list-iteration loop, it proceeds to the next word in the list. In a C-style loop, it proceeds directly to the evaluation of the step expression (expr3). Ifnis specified, it resumes iteration at thenth enclosing loop.
Master Bash with Deep Grasping Methodology!Learn More





