opscape

Text processing

seq

Print a sequence of numbers, with a chosen start, step, separator and format.

seq [OPTION...] [FIRST [INCREMENT]] LAST

13 of 13 examples run in Debian 13.7 on 2026-10-31 · coreutils 9.7-3 · manual: seq(1)

seq prints numbers from FIRST to LAST, one per line, counting up by INCREMENT. With one argument it counts from 1; with two, from FIRST in steps of 1; with three, the middle one is the step, which may be negative or fractional.

In bash, brace expansion ({1..10}, {0..100..5}) covers many of the same cases without starting a process. seq is still the better choice when the bounds are in variables ({1..$n} does not work), when you need fractions or printf-style formatting, or in a POSIX sh script. It also makes quick test data: seq 100000 > big.txt.

Examples

Count from 1 to N

seq 5
explain
output
1
2
3
4
5

With a single argument seq starts at 1 and prints each number on its own line.

Choose the start and the step

seq 2 2 10 | paste -s -d ' '
explain
output
2 4 6 8 10

The middle argument is the increment, so this counts from 2 to 10 in steps of 2.

Count down

seq 10 -3 1 | paste -s -d ' '
explain
output
10 7 4 1

A negative increment counts down. The sequence stops at the last value that does not pass the limit, here 1.

Numbers on one line

seq -s , 5
explain
output
1,2,3,4,5

-s , puts a comma between the numbers instead of a newline.

Pad with zeros to equal width

seq -w 8 11 | paste -s -d ' '
explain
output
08 09 10 11

-w pads every number with leading zeros to the width of the widest one: 08 09 10 11.

Generate file names

seq -f 'backup-%03g.tar' 3
explain
output
backup-001.tar
backup-002.tar
backup-003.tar

-f takes a printf-style floating-point format; %03g gives three-digit, zero-padded numbers.

Fractional steps

seq 0 0.25 1
explain
output
0.00
0.25
0.50
0.75
1.00

The step can be a fraction. seq prints all numbers with as many decimals as the most precise argument needs.

Fractions are floating point underneath

seq -f '%.20g' 0 0.1 0.3
explain
output
0
0.1
0.2
0.30000000000000000001

With 20 significant digits the last value shows as 0.30000000000000000001: 0.1 has no exact binary representation. The default format hides this by rounding to one decimal.

Loop with bounds from a variable

n=3
for i in $(seq "$n"); do echo "attempt $i of $n"; done
output
attempt 1 of 3
attempt 2 of 3
attempt 3 of 3

{1..$n} would not expand, because brace expansion happens before variables are substituted. $(seq "$n") produces the list at run time.

Build an arithmetic expression

seq -s + 100 | bc
explain
output
5050

seq -s + writes 1+2+...+100 and bc evaluates it: 5050.

Make test data quickly

seq 100000 > big.txt
wc -l big.txt
tail -n 1 big.txt
output
100000 big.txt
100000

A hundred thousand numbered lines in a fraction of a second, handy for trying out sort, split or head on a bigger file.

When LAST is smaller than FIRST

seq 3 1
echo "exit status: $?"
output
exit status: 0

Without a negative step, counting up from 3 to 1 produces no numbers at all, and that is not an error: the exit status is 0. Write seq 3 -1 1 to count down.

A non-numeric argument

seq 1 ten
explain
outputexit 1
seq: invalid floating point argument: ‘ten’
Try 'seq --help' for more information.

seq only accepts numbers and stops with an error and status 1.

Common pitfalls

  • seq 10 1 prints nothing. Counting down needs an explicit negative step: seq 10 -1 1.

  • Fractions are binary floating-point values. The default output rounds them to the precision of your arguments, but a custom -f format with many digits exposes the error. Scale to integers when exact values matter.

  • for i in $(seq 1000000) builds the whole list in memory first. Use seq ... | while read -r i or bash's C-style for ((i=1; i<=n; i++)) for big ranges.

GNU vs BSD and macOS

The examples above ran the GNU/Linux version. These are the differences that matter on macOS and the BSDs (written from their documentation, not run here).

  • macOS has seq (since 10.7) with -s, -w and -f. Older BSDs used jot instead: jot 5 counts 1 to 5.

Options worth knowing

A selection, described in our own words. For the full list, see seq(1) or seq --help.

OptionWhat it does
-s, --separator STRINGSeparate the numbers with STRING instead of a newline.
-w, --equal-widthPad numbers with leading zeros to the same width.
-f, --format FORMATFormat each number with a printf-style floating-point FORMAT such as %03g.

Related