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
| Option | What it does |
|---|---|
-w | equalize width with leading zeros: 08 09 10 |
-s STRING | separator between numbers instead of \n (a \n is still added at the end) |
-f FORMAT | printf-style floating-point number format: %g, %f, %e with width and precision; text can be added around it |
Formats for -f
| Format | Example | Result |
|---|---|---|
%g | seq -f %g 1 3 | 1 2 3 (the default for numbers like 1e3) |
%.2f | seq -f %.2f 0 0.5 1 | 0.00 0.50 1.00 |
%03.0f | seq -f %03.0f 8 10 | 008 009 010 |
%5.1f | seq -f %5.1f 1 2 | 1.0 2.0 |
text%gtext | seq -f 'file%g.txt' 2 | file1.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 2means "from 1 to 2 with step 10" and prints only1. Correct:seq 1 2 10. seq 5 1silently prints nothing and returns 0: the default step is+1even when the end is less than the start. For counting down writeseq 5 -1 1.- A step of 0 is an error (
invalid Zero increment value). -f %ggives an exponent for large numbers:seq -f 'id-%g' 1000000 1000000→id-1e+06. For integers in a format use%.0for%06.0f.-wand-fcan'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.95ends at0.9, andseq 1 3 8at7. - 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 needseq 1 "$n".
See also
shuf · xargs · paste · loops · arithmetic · echo and printf