seq
The seq
command is a handy tool to generate a sequence of numbers in ascending or descending order. Both integer and floating-point numbers are supported. You can also customize the formatting for numbers and the separator between them.
Integer sequences
You need three numbers to generate an arithmetic progression — start, step and stop. When you pass only a single number as the stop value, the default start and step values are assumed to be 1
.
# start=1, step=1 and stop=3
$ seq 3
1
2
3
Passing two numbers are considered as start and stop values (in that order).
# start=25434, step=1 and stop=25437
$ seq 25434 25437
25434
25435
25436
25437
# start=-5, step=1 and stop=-3
$ seq -5 -3
-5
-4
-3
When you want to specify all the three numbers, the order is start, step and stop.
# start=1000, step=5 and stop=1010
$ seq 1000 5 1010
1000
1005
1010
By using a negative step value, you can generate sequences in descending order.
# no output
$ seq 3 1
# need to explicitly use a negative step value
$ seq 3 -1 1
3
2
1
$ seq 5 -5 -10
5
0
-5
-10
Floating-point sequences
Since 1
is the default start and step values, you need to change at least one of them to get floating-point sequences.
$ seq 0.5 3
0.5
1.5
2.5
$ seq 0.25 0.33 1.12
0.25
0.58
0.91
E scientific notation is also supported.
$ seq 1.2e2 1.22e2
120
121
122
$ seq 1.2e2 0.752 1.22e2
120.000
120.752
121.504
Customizing separator
You can use the -s
option to change the separator between the numbers of a sequence. Multiple characters are allowed. Depending on your shell you can use ANSI-C quoting to use escapes like \t
instead of a literal tab character. A newline is always added at the end of the output.
$ seq -s' ' 4
1 2 3 4
$ seq -s: -2 0.75 3
-2.00:-1.25:-0.50:0.25:1.00:1.75:2.50
$ seq -s' - ' 4
1 - 2 - 3 - 4
$ seq -s$'\n\n' 4
1
2
3
4
Leading zeros
By default, the output will not have leading zeros, even if they are part of the numbers passed to the command.
$ seq 008 010
8
9
10
The -w
option will equalize the width of the output numbers using leading zeros. The largest width between the start and stop values will be used.
$ seq -w 8 10
08
09
10
$ seq -w 0003
0001
0002
0003
printf style formatting
You can use the -f
option for printf
style floating-point number formatting. See bash manual: printf for more details on formatting options.
$ seq -f'%g' -s: 1 0.75 3
1:1.75:2.5
$ seq -f'%.4f' -s: 1 0.75 3
1.0000:1.7500:2.5000
$ seq -f'%.3e' 1.2e2 0.752 1.22e2
1.200e+02
1.208e+02
1.215e+02
Limitations
As per the manual:
On most systems,
seq
can produce whole-number output for values up to at least2^53
. Larger integers are approximated. The details differ depending on your floating-point implementation.
# example with approximate values
$ seq 100000000000000000000 3 100000000000000000010
100000000000000000000
100000000000000000000
100000000000000000008
100000000000000000008
However, note that when limited to non-negative whole numbers, an increment of
1
and no format-specifying option,seq
can print arbitrarily large numbers.
# no approximation for step value of 1
$ seq 100000000000000000000000000000 100000000000000000000000000005
100000000000000000000000000000
100000000000000000000000000001
100000000000000000000000000002
100000000000000000000000000003
100000000000000000000000000004
100000000000000000000000000005