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 continue statement is a built-in loop control command in Bash that immediately terminates the current iteration of an enclosing for, while, until, or select loop. Upon execution, it bypasses any remaining commands within the loop’s body and transfers control back to the loop’s condition evaluator, incrementer, or prompt to begin the next iteration.
continue [n]

Parameter Mechanics

  • n (Optional): An integer greater than or equal to 1 that specifies the depth of the loop hierarchy to target.
  • Default Behavior: If n is omitted, it defaults to 1, meaning the statement applies to the innermost enclosing loop.
  • Nested Targeting: If n is provided (e.g., continue 2), Bash skips the remaining execution of the current loop and the remaining execution of the n-1 enclosing loops, resuming at the next iteration of the nth enclosing loop.
  • Out-of-Bounds n: If the specified n is greater than the total number of enclosing loops, the statement targets the outermost loop.

Execution Flow Visualization

Standard Continue (n=1) In a single-level loop, continue skips the remaining commands in the block and forces the loop to evaluate the next item or condition.
for i in {1..3}; do
    echo "Start iteration $i"
    
    if [[ $i -eq 2 ]]; then
        continue
    fi
    
    echo "End iteration $i"  # Bypassed when i equals 2
done
Nested Continue (n=2) In a nested loop architecture, passing an integer dictates which loop’s iteration is advanced.
outer=0
while [[ $outer -lt 2 ]]; do
    ((outer++))
    echo "Outer loop: $outer"
    
    for inner in {1..2}; do
        echo "  Inner loop: $inner"
        
        if [[ $inner -eq 1 ]]; then
            continue 2
        fi
        
        echo "  Inner end"  # Bypassed when inner equals 1
    done
    
    echo "Outer end"        # Bypassed when inner equals 1
done
In the nested example, executing continue 2 immediately halts the for loop, skips the remaining commands in the while loop block, and triggers the next iteration of the while loop.

Exit Status

The continue command yields an exit status of 0 (success) when executed within a valid loop context. If invoked outside of a loop structure, Bash triggers a runtime warning message (e.g., bash: continue: only meaningful in a 'for', 'while', or 'until' loop) but still returns an exit status of 0, and the script will continue executing subsequent lines. The continue command only returns a non-zero exit status if the provided n argument is less than 1 (e.g., continue 0) or is a non-numeric value.
Master Bash with Deep Grasping Methodology!Learn More