Three ways to loop over a range of numbers in Bash: brace expansion, seq and the C-style for loop, plus why a range stored in a variable does not expand.
The short answer
#For a fixed range, use brace expansion. When the start or end comes from a variable, use a C-style for loop, because brace expansion does not expand variables.
_11# Fixed range_11for i in {1..5}; do_11 echo "$i"_11done_11_11# Range from variables_11start=1_11end=5_11for (( i = start; i <= end; i++ )); do_11 echo "$i"_11done
Why it happens
#The most common surprise is this:
_10end=5_10for i in {1..$end}; do_10 echo "$i"_10done_10# prints: {1..5}
Bash performs brace expansion before it expands variables. When it sees {1..$end}, the text is not a valid sequence yet, so it is left alone. Then $end becomes 5, and you are left with the literal string {1..5}, which the loop prints once.
So the tool you pick depends on where the numbers come from. Brace expansion is a text-generation feature that runs early and only sees literal numbers. The C-style loop and seq both work with values worked out at run time.
How to fix it
#C-style for loop (recommended for variables)
This is built into Bash, needs no external command and handles any integer bounds.
_10count=10_10_10for (( i = 0; i < count; i++ )); do_10 echo "Item $i"_10done_10_10# Count down in steps of 2_10for (( i = 10; i >= 0; i -= 2 )); do_10 echo "$i"_10done
Inside the double parentheses you can write variable names without a dollar sign, and the comparison operators mean what they mean in maths. It works in every Bash version you are likely to meet, including the Bash 3.2 that ships with macOS.
Brace expansion (fixed ranges)
_10for i in {1..5}; do echo "$i"; done_10_10# Zero-padded: 01 02 ... 10_10for i in {01..10}; do echo "file-$i.txt"; done_10_10# Letters_10for c in {a..e}; do echo "$c"; done_10_10# With a step_10for i in {0..20..5}; do echo "$i"; done
The step form and zero padding need Bash 4 or later. On macOS's built-in Bash 3.2 they do not work as shown, so use the C-style loop or seq there. Brace expansion also builds the whole list in memory before the loop starts, so avoid it for very large ranges.
seq
seq is an external command available on Linux and macOS. It is handy when you want a step, decimals or padding with variables.
_12start=1_12end=10_12_12for i in $(seq "$start" "$end"); do_12 echo "$i"_12done_12_12# Step of 2_12for i in $(seq 1 2 9); do echo "$i"; done_12_12# Equal width: 01 02 ... 10_12for i in $(seq -w 1 10); do echo "$i"; done
Note the argument order: start, step, end. seq is not part of Bash, so very minimal systems may not have it, and starting a process is slower than a built-in loop.
while loop
A while loop with arithmetic is useful when the stopping condition is more than a simple bound.
_10i=1_10while (( i <= 5 )); do_10 echo "$i"_10 (( i++ ))_10done
Common pitfalls
#- Variables in braces.
{1..$n}never works. Usefor (( i = 1; i <= n; i++ )). Avoidevalas a workaround; it runs any text in the variable as code. - Off-by-one errors. Brace ranges and
seqinclude both ends. In a C-style loop, choose between the less-than and less-than-or-equal comparison deliberately. (( i++ ))underset -e****. Wheniis 0, the expression evaluates to 0, which counts as failure and stops a script usingset -e. Use(( i += 1 ))ori=$(( i + 1 )), which avoid the problem in that position.- Running the script with
sh****. Brace expansion and(( ))are Bash features. Under dash,{1..5}stays as literal text. - Decimals. Bash arithmetic is integer only. Use
seq 0 0.5 2if you need fractional steps.
Related reading
#Was this resource helpful?