Bootstrap FreeKB - Linux Commands - seq (sequence of numbers)
Linux Commands - seq (sequence of numbers)

Updated:   |  Linux Commands articles

The seq (sequence) command can be used to produce a string of numbers. For example, seq 1 5 will print the numbers 1, 2, 3, 4, and 5.

[john.doe@server1 ~]# seq 1 5
1
2
3
4
5

 

Simiarly, the following does the same.

[john.doe@server1 ~]# seq 5
1
2
3
4
5

 


Display the results on a single line

The -s or --separator option can be used to choose the separator you would like to use. By default, the \n (new line) separator is used. In this example, the separator is a single space, which produces the sequence on a single line.

[john.doe@server1 ~]# seq -s " " 5
1 2 3 4 5

 


Format the output

The -f or --format option can be used to format the output.

[john.doe@server1 ~]# seq -f "%a" 5
0x8p-a
0x8p-2
0xcp-2
0x8p-1
0xap-1
[john.doe@server1 ~]# seq -f "%e" 5
1.000000e+00
2.000000e+00
3.000000e+00
4.000000e+00
5.000000e+00
[john.doe@server1 ~]# seq -f "%f" 5
1.000000
2.000000
3.000000
4.000000
5.000000

 


Equal width

The -w or --equal-width option can be used to display the output with equal width. For example, the seq 10 command with not be equal width. In this example, numbers 1 through 9 have a width of 1 character, and the number 10 has a width of 2 characters.

[john.doe@server1 ~]# seq 10
1
2
3
4
5
6
7
8
9
10

 

The -w or --equal-width option makes it so that the output is equal width.

[john.doe@server1 ~]# seq -w 0 10
01
02
03
04
05
06
07
08
09
10

 


For loop

The seq command can be used in a for loop. This bash shell script will produce the same example output as the seq 5 command.

#!/bin/bash
for foo in $(seq 5)
do
  echo $foo
done

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 0ba07a in the box below so that we can be sure you are a human.