Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A 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.
for variable_name in word_list; do
    # commands to execute
done
Mechanics:
  1. Shell Expansions: Before the loop initiates, the shell evaluates the word_list through 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.
  2. Assignment and Execution: For each resulting word from the final expanded list, the shell assigns the word’s value to variable_name and executes the commands enclosed between the do and done keywords.
  3. Implicit Iteration: If the in word_list clause 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.
for (( expr1; expr2; expr3 )); do
    # commands to execute
done
Mechanics:
  1. Initialization (expr1): This arithmetic expression is evaluated exactly once before the loop begins. It is typically used to initialize a loop counter variable.
  2. 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. If expr2 is omitted, it evaluates to true.
  3. Step (expr3): This expression is evaluated at the end of each iteration, immediately after the commands within the dodone block have executed. It is typically used to increment or decrement the loop counter.

Loop Control Statements

The execution flow within both types of for loops can be modified using built-in control statements:
  • break [n]: Immediately terminates the loop. If n is specified, it breaks out of n nested 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). If n is specified, it resumes iteration at the nth enclosing loop.
Master Bash with Deep Grasping Methodology!Learn More