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.
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
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
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' 3
1
2
3
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 0002
0001
0002
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
As per the manual:
On most systems, seq can produce whole-number output for values up to at least 2^53. Larger integers are approximated. The details differ depending on your floating-point implementation.
# example with approximate values
$ seq 100000000000000000000 3333 100000000000000010000
100000000000000000000
100000000000000003336
100000000000000006664
100000000000000010000
However, when limited to non-negative whole numbers, an increment of less than 200, and no format-specifying option, seq can print arbitrarily large numbers.
# no approximation for smaller step values
$ seq 100000000000000000000000000000 100000000000000000000000000005
100000000000000000000000000000
100000000000000000000000000001
100000000000000000000000000002
100000000000000000000000000003
100000000000000000000000000004
100000000000000000000000000005
The exercises directory has all the files used in this section.
1) Generate numbers from 42 to 45 in ascending order.
##### add your solution here
42
43
44
45
2) Why does the command shown below produce no output?
# no output
$ seq 45 42
# expected output
##### add your solution here
45
44
43
42
3) Generate numbers from 25 to 10 in descending order, with a step value of 5.
##### add your solution here
25
20
15
10
4) Is the sequence shown below possible to generate with seq? If so, how?
##### add your solution here
01.5,02.5,03.5,04.5,05.5
5) Modify the command shown below to customize the output numbering format.
$ seq 30.14 3.36 40.72
30.14
33.50
36.86
40.22
##### add your solution here
3.014e+01
3.350e+01
3.686e+01
4.022e+01