---
title: "How do I loop over a range of numbers in Bash?"
description: "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."
url: https://www.danny.engineering/answer/bash/how-do-i-loop-over-a-range-of-numbers-in-bash
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T20:31:00.000Z
tags: ["Bash"]
---

# How do I loop over a range of numbers in Bash?

## 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.


```bash
# Fixed range
for i in {1..5}; do
  echo "$i"
done

# Range from variables
start=1
end=5
for (( i = start; i <= end; i++ )); do
  echo "$i"
done
```


## Why it happens


The most common surprise is this:


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


```bash
count=10

for (( i = 0; i < count; i++ )); do
  echo "Item $i"
done

# Count down in steps of 2
for (( i = 10; i >= 0; i -= 2 )); do
  echo "$i"
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)


```bash
for i in {1..5}; do echo "$i"; done

# Zero-padded: 01 02 ... 10
for i in {01..10}; do echo "file-$i.txt"; done

# Letters
for c in {a..e}; do echo "$c"; done

# With a step
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.


```bash
start=1
end=10

for i in $(seq "$start" "$end"); do
  echo "$i"
done

# Step of 2
for i in $(seq 1 2 9); do echo "$i"; done

# Equal width: 01 02 ... 10
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.


```bash
i=1
while (( i <= 5 )); do
  echo "$i"
  (( i++ ))
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.

## Related reading

- [GNU Bash manual: Brace expansion](https://www.gnu.org/software/bash/manual/html_node/Brace-Expansion.html)
- [GNU Bash manual: Looping constructs](https://www.gnu.org/software/bash/manual/html_node/Looping-Constructs.html)
