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.
Print sequence of numbers till an upper limit
In the simplest form, you specify an upper limit to seq and it will print the sequence beginning from 1 till the upper limit.
Here's an example:
[email protected]:~$ seq 4 1 2 3 4
Print sequence between two numbers (lower and upper limit)
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:
[email protected]:~$ seq 3 6 3 4 5 6
Print sequence between a limit but with custom increment
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.
[email protected]:~$ seq 3 0.5 6 3.0 3.5 4.0 4.5 5.0 5.5 6.0
Print a sequence backward
Another trick is to print a sequence backward. To do that, you have to specify a negative increment.
[email protected]:~$ seq 6 -1 4 6 5 4
A screenshot of all the above examples:
What happens when you put something like 0.7? In that case, the upper limit will not be breached.
[email protected]:~$ 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.
Print sequence with same width
w with seq command is used to keep the same width for the printed numbers.
[email protected]:~$ seq -w 9 11 09 10 11
Print sequence in a specific format
You may format the output line in a specified format with the option
[email protected]:~$ 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.
Print sequence with string as separator
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
[email protected]:~$ 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.
[email protected]:~$ 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.