Text processing
seq
Print a sequence of numbers, with a chosen start, step, separator and format.
seq [OPTION...] [FIRST [INCREMENT]] LAST
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
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 ' '
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 ' '
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
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 ' '
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
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
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
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
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
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
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: $?"
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
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 1prints 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
-fformat 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. Useseq ... | while read -r ior bash's C-stylefor ((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,-wand-f. Older BSDs usedjotinstead:jot 5counts 1 to 5.
Options worth knowing
A selection, described in our own words. For the full list, see seq(1) or seq --help.
| Option | What it does |
|---|---|
-s, --separator STRING | Separate the numbers with STRING instead of a newline. |
-w, --equal-width | Pad numbers with leading zeros to the same width. |
-f, --format FORMAT | Format each number with a printf-style floating-point FORMAT such as %03g. |
Related
shufShuffle lines into random order, or pick random lines, numbers or words.printfPrint text and values through a format string, with precise control over spacing, numbers and escapes.bcAn arbitrary-precision calculator language, used from scripts for decimal math the shell cannot do.- Brace expansionGenerate lists of words with {a,b,c} and sequences with {1..10} and {01..10..2}, why {1..$n} does not work, and how braces differ from globs.
- Loopsfor over lists, files and ranges, C-style for, while and until, break and continue, retry loops, and redirecting a whole loop's input or output.