Skip to main content
Commands

Seq Command in Linux

Print a sequence of numbers with specified increment or format with seq command.

Abhishek Prakash

Warp Terminal

The seq command, short for sequence, is used for printing a sequence of numbers. The numbers could be integers or real (with decimal points).

Let's see how you can use this command with some examples.

Using seq command

You can use seq without options for producing sequence of numbers in 3 different formats.

In the simplest form, you specify an upper limit to seq and it will print the sequence beginning from 1 till the upper limit.

seq n

Here's an example:

abhishek@lhb:~$ seq 4
1
2
3
4

You can provide two numbers in ascending order and it will print the sequence starting from the lower number till the upper one.

seq n1 n2

Have a look at this example:

abhishek@lhb:~$ seq 3 6
3
4
5
6

So far, the increment in the sequence has been one. But you may also define a custom increment between the lower and upper limit.

seq n1 inc n2

The incremental value can be an integer or a decimal value.

abhishek@lhb:~$ seq 3 0.5 6
3.0
3.5
4.0
4.5
5.0
5.5
6.0

Another trick is to print a sequence backward. To do that, you have to specify a negative increment.

abhishek@lhb:~$ seq 6 -1 4
6
5
4

A screenshot of all the above examples:

seq command example

What happens when you put something like 0.7? In that case, the upper limit will not be breached.

abhishek@lhb:~$ seq 3 0.7 6
3.0
3.7
4.4
5.1
5.8

So far, you have not used any options with seq command. Let's see and use them as well.

The option w with seq command is used to keep the same width for the printed numbers.

abhishek@lhb:~$ seq -w 9 11
09
10
11

You may format the output line in a specified format with the option f.

abhishek@lhb:~$ seq -f '##%g##' 3 5
##3##
##4##
##5##

%g is used for default numbers. %e to display the number in exponential format and %f in floating-point format.

So far, the sequence were all printed vertically. That's because by default, the separator is the new line character. You may change that with option s.

abhishek@lhb:~$ seq -s ':' 4
1:2:3:4

The ' before the separator is not necessary but it is good for avoiding bad surprises.

Practical use of seq command

You may wonder what could be a practical use of this seq command. There could be numerous situations where you could use it.

One particular example I can think of is when you use for loop in bash. Instead of specifying the sequence manually in the loop condition, you can use the seq command.

#!/bin/bash

for i in $(seq 4 2 18)
do
	echo "Number $i"
done

When you run the above bash script, it will loop on the given sequence and print the values.

abhishek@lhb:~$ bash seq.sh
Number 4
Number 6
Number 8
Number 10
Number 12
Number 14
Number 16
Number 18

That's pretty much all the important stuff you need to know about the seq command. If you want more details, you can always use its man page.

Abhishek Prakash