← Full reference

seq

A sequence of numbers — with a step, counting down, fractions, leading zeros and a custom format

seq prints a series of numbers from start to end with a given step, one per line. It's used to drive loops (for i in $(seq 10)), generate test data, numbered file names and ranges for xargs.

Syntax

seq [options] LAST                # from 1 to LAST
seq [options] FIRST LAST          # step 1
seq [options] FIRST STEP LAST     # custom step; negative — counting down

The step is the middle argument. All three numbers can be fractional and negative. The series ends when the next number would go past LAST; LAST itself is printed only if it is hit exactly.

Options

OptionWhat it does
-wequalize width with leading zeros: 08 09 10
-s STRINGseparator between numbers instead of \n (a \n is still added at the end)
-f FORMATprintf-style floating-point number format: %g, %f, %e with width and precision; text can be added around it

Formats for -f

FormatExampleResult
%gseq -f %g 1 31 2 3 (the default for numbers like 1e3)
%.2fseq -f %.2f 0 0.5 10.00 0.50 1.00
%03.0fseq -f %03.0f 8 10008 009 010
%5.1fseq -f %5.1f 1 2 1.0 2.0
text%gtextseq -f 'file%g.txt' 2file1.txt file2.txt
%%seq -f '%%%g' 1 1%1 — a percent sign in the output

Integer %d, %x aren't supported: seq works with floating-point numbers. Without -f the format is chosen automatically: by the number of digits after the point in the input numbers (seq 0 0.25 1 → 0.00 0.25 …).

Examples

# 1..5
seq 5

# from 2 to 10 with step 2
seq 2 2 10

# counting down — the step must be negative
seq 5 -1 1

# on one line, comma- or space-separated
seq -s, 5
seq -s ' ' 3

# with leading zeros
seq -w 1 10

# fractions
seq 0 0.25 1
seq -f '%.1f' 1 0.5 3

# file names: file001.txt … file005.txt
seq -f 'file%03g.txt' 5

# loop
for i in $(seq 3); do echo "attempt $i"; done

# create 100 files with a single touch call
seq -f 'part%03g.log' 100 | xargs touch

# sum of 1..100 (no bc in the sandbox)
echo $(( $(seq -s+ 100) ))

# negative numbers
seq -3 -1

Pitfalls

  • The step is the second argument, not the third. seq 1 10 2 means "from 1 to 2 with step 10" and prints only 1. Correct: seq 1 2 10.
  • seq 5 1 silently prints nothing and returns 0: the default step is +1 even when the end is less than the start. For counting down write seq 5 -1 1.
  • A step of 0 is an error (invalid Zero increment value).
  • -f %g gives an exponent for large numbers: seq -f 'id-%g' 1000000 1000000 → id-1e+06. For integers in a format use %.0f or %06.0f.
  • -w and -f can't be combined (format string may not be specified when printing equal width strings). For zeros with text use -f '%03g'.
  • The end is printed only if hit exactly. seq 0 0.1 0.95 ends at 0.9, and seq 1 3 8 at 7.
  • For simple integer ranges bash has {1..10} and {01..10} (with zeros) — no extra process. But you can't use variables in braces: {1..$n} won't expand, and that's when you need seq 1 "$n".

See also

shuf · xargs · paste · loops · arithmetic · echo and printf