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.

The Bash arithmetic for loop, commonly referred to as the C-style for loop, is a control flow statement that utilizes Bash’s arithmetic evaluation context (( )) to iterate based on numerical expressions. It evaluates three distinct arithmetic expressions to control initialization, loop continuation, and iteration stepping.
for (( expr1; expr2; expr3 )); do
    # statements
done

Component Breakdown

  • expr1 (Initialization): An arithmetic expression evaluated exactly once before the loop begins. While typically used to initialize loop control variables, Bash does not enforce this syntactically or semantically. It can contain any valid arithmetic operation, discard its result, or be omitted entirely.
  • expr2 (Condition): Evaluated before each iteration. If the arithmetic evaluation yields a non-zero value (true), the loop body executes. If it evaluates to 0 (false), the loop terminates. If omitted, it defaults to 1 (creating an infinite loop).
  • expr3 (Step): Evaluated at the end of each iteration, immediately after the loop body executes. It is typically used to mutate loop control variables (e.g., incrementing or decrementing).

Execution Context and Rules

Because the loop parameters are enclosed in double parentheses (( )), they are parsed under Bash’s arithmetic evaluation rules (as an arithmetic compound command), rather than standard shell word splitting or globbing rules. This is distinct from arithmetic expansion ($(( ))), which substitutes the evaluated result back into the command line.
  • Variable Dereferencing: Variables inside the arithmetic evaluation context do not require the $ prefix for parameter expansion. Using i < limit is syntactically valid and safer than $i < $limit. If limit is unset or empty, (( i < limit )) safely evaluates as (( 0 < 0 )). Conversely, (( i < $limit )) expands the variable before evaluation, resulting in a syntax error ((( 0 < )) throws operand expected).
  • Supported Operators: The context supports standard C-style mathematical and relational operators, including pre/post-increment (++i, i++), pre/post-decrement (--i, i--), compound assignment (+=, -=, *=), and bitwise operations.
  • Comma Operator: Multiple expressions can be evaluated within a single component (expr1, expr2, or expr3) by separating them with a comma ,. When used in the condition (expr2), the expressions are evaluated from left to right, and the right-most expression determines the final truth value of the condition.

Syntax Visualizations

Standard Iteration:
for (( i = 0; i < 10; i++ )); do
    echo "$i"
done
Multiple Variables and Comma Operator in Condition:
for (( i = 0, j = 100; i <= 50, j > 0; i++, j -= 2 )); do
    # The loop continues as long as j > 0 is true, 
    # regardless of the evaluation of i <= 50.
    echo "i: $i, j: $j"
done
Omitted Expressions (Infinite Loop):
for (( ; ; )); do
    # Requires an internal break statement to terminate
    break 
done
Dynamic Condition Evaluation:
limit=5
for (( i = 1; i <= limit * 2; i += 2 )); do
    echo "$i"
done
Master Bash with Deep Grasping Methodology!Learn More