More Bash answers

by Danny
Published 2 min read

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
_11
for i in {1..5}; do
_11
echo "$i"
_11
done
_11
_11
# Range from variables
_11
start=1
_11
end=5
_11
for (( i = start; i <= end; i++ )); do
_11
echo "$i"
_11
done

Why it happens

#

The most common surprise is this:


_10
end=5
_10
for i in {1..$end}; do
_10
echo "$i"
_10
done
_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

#

This is built into Bash, needs no external command and handles any integer bounds.


_10
count=10
_10
_10
for (( i = 0; i < count; i++ )); do
_10
echo "Item $i"
_10
done
_10
_10
# Count down in steps of 2
_10
for (( i = 10; i >= 0; i -= 2 )); do
_10
echo "$i"
_10
done

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)


_10
for i in {1..5}; do echo "$i"; done
_10
_10
# Zero-padded: 01 02 ... 10
_10
for i in {01..10}; do echo "file-$i.txt"; done
_10
_10
# Letters
_10
for c in {a..e}; do echo "$c"; done
_10
_10
# With a step
_10
for 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.


_12
start=1
_12
end=10
_12
_12
for i in $(seq "$start" "$end"); do
_12
echo "$i"
_12
done
_12
_12
# Step of 2
_12
for i in $(seq 1 2 9); do echo "$i"; done
_12
_12
# Equal width: 01 02 ... 10
_12
for 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.


_10
i=1
_10
while (( i <= 5 )); do
_10
echo "$i"
_10
(( i++ ))
_10
done

Common pitfalls

#
  • Variables in braces. {1..$n} never works. Use for (( i = 1; i <= n; i++ )). Avoid eval as a workaround; it runs any text in the variable as code.
  • Off-by-one errors. Brace ranges and seq include both ends. In a C-style loop, choose between the less-than and less-than-or-equal comparison deliberately.
  • (( i++ )) under set -e****. When i is 0, the expression evaluates to 0, which counts as failure and stops a script using set -e. Use (( i += 1 )) or i=$(( 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 2 if you need fractional steps.
#

Was this resource helpful?