The command “seq” can be used to generate a sequence of numbers in bash. The following is the syntax of “seq” command.

seq LAST

seq FIRST LAST

seq FIRST INCREMENT LAST

Look at the following to understand how it works.

Example 1:

[sara@sara-desktop ~]$ seq 5
1
2
3
4
5
[sara@sara-desktop ~]$

Example 2:

[sara@sara-desktop ~]$ seq 5 10
5
6
7
8
9
10
[sara@sara-desktop ~]$

Example 3:

[sara@sara-desktop ~]$ seq 0 10 50
0
10
20
30
40
50
[sara@sara-desktop ~]$

Generally the “seq” command prints one number per line as it assumes “\n” as the separator. However the character used as separator can be changed using “-s” option. In the following example, all numbers are separated by a space character.

[sara@sara-desktop ~]$ seq -s " " 0 10 50
0 10 20 30 40 50
[sara@sara-desktop ~]$

Now let us see a practical use of this in a bash script.

[sara@sara-desktop ~]$ for i in `(seq 5)`; do echo iteration $i; done
iteration 1
iteration 2
iteration 3
iteration 4
iteration 5
[sara@sara-desktop ~]$