C-Style Loops in Bash

Use arithmetic for loops with counters and step values.

macOSbashshellscriptingLow risk
Write for ((i=1;i<=5;i++)) to loop five times with a counter.

01Command

Command
$ for ((i=1;i<=5;i++)); do echo "step $i"; done

02What it does

The C-style for loop gives scripts an explicit counter without seq or external tools, and supports any arithmetic expression such as i+=2. It is the clearest way to retry an operation or iterate over a numeric range.

03Risk

RiskLow risk
RequirementsmacOS default zsh/bash (bash 3.2+; some features need bash 4+ via Homebrew)
CompatibilitymacOS with bash 3.2+ (GNU bash 4+ via Homebrew for advanced features)
!Warning: Bash arithmetic is integer-only; use bc or awk for floating-point counters.

04Undo

✓No undo needed — this command does not change persistent state.

05Sources

Last verified: 2026-10-13 · Category: Developer