The Linux seq command has been around for years, since 1985 to be exact. However, it is still one of those useful commands that many Linux users rarely use. The seq command prints a sequential list of integers or floating point numbers suitable for use in scripts and piping to other commands. With the seq command, you can control the start and end of the number sequence along with an increment. It also provides several formatting options. However, there is one limitation of the seq command is that it only works with the number, not letters.
The seq command is part of the GNU Core Utilities package that is available in just about any Linux system. In this article, we will see how to use the seq command to generate a sequence of numbers along with some examples and use cases.
Seq Command Syntax
Here is the basic syntax of the seq command:
In the above syntax, the [OPTION], FIRST and INCREMENT arguments are optional. The default value of FIRST and INCREMENT argument is always 1.
Basic Use of Seq Command
The basic use of the seq command will print the sequential numbers from 1 to the last number you specify.
It will take the default increment of 1.
For example, to print the sequence of numbers from 1 to 5, specify 5 as the value of the LAST argument.
Specify Starting Number in Sequence
To start the sequence with a number other than 1, you can do:
The FIRST will be used as a starting number and LAST will be used as ending number in the generated list.
In the following example, we have printed the sequence of numbers from 3 to 5.
Specify The Increment Value
To print the sequence numbers with a fixed increment other than 1, use the following syntax:
In the INCREMENT, specify the number you want to increment by.
In the following example, we have printed the sequence of numbers from 2 to 10 with an increment of 2.
Floating point numbers are also supported while generating the sequence number:
Print Sequential Numbers in Reverse Order
To print sequence numbers in reverse order (from larger to smaller number), use negative increment.
In the following example, we have printed the sequence of numbers from 10 to 2 with a decrement of 2.
Seq Command Options
Print Numbers with Printf Format Specifiers
With the -f option, Seq command allows to use printf style format for printing the numbers using the %e , %f , %g , %E , %F , %G as the conversion characters. However, the %g is used as default.
In the following example, we have printed the Serial number from 1 to 5 using %g as the conversion character.
In the following example, we have printed the version number in floating pint format from 1 to 5 using %f as the conversion character. The “%.2f” prints the floating numbers with 2 digits after the decimal separator.
In the following example, we have printed the five sequential dates from 1 st May to 5 th May 2020.
Print Numbers with Same Width
To print the sequence number in the same width, use the -w option with seq command. It will add leading zeros to match the width of the largest number.
In the following example ,seq adds two leading zeros to the single digit numbers to match the width of the largest number (100).
Add Separator Between Sequential Numbers
The Seq command by default uses /n (newline) as a separator between the sequence numbers. This cause each number to print on separate line. You can specify any character as a separator between numbers like dot, comma, hyphen, etc.
To add a separator between the numbers, use -s option followed by the desired character.
In the following example, we have separated the numbers by commas using the -s , option:
In the following example, we have separated the number with spaces by using the -s” ” .
Seq Command Use Cases
Performing Basic Calculations
You can also perform simple calculations with seq command like addition, subtraction, multiplication, and division. For this, you will need to use any of the + , – , * and / separator and then pipe the output to the command-line calculator bc.
For example, the following command outputs the numbers from 2 to 10, incremented by 2 and separated by a + sign.
If we pipe the above output to the bc, it will perform the addition of these numbers like this:
Creating and Removing Files
With the seq command, you can create multiple files at once with sequential numbers added to the file name. In the following example, we have created three files with named file1, file2, and file3 using the seq command:
Similarly, to remove the file, you can use
Writing the Sequence Number in a File
You can save the sequence number in a file rather than printing them to the shell using simple redirection.
For example, the following command prints the sequence numbers from 1 to 5:
You can save the output in a file by using the > to redirect the output to a file, like so:
Use Seq in For Loop
We can use the seq command with “for loops”. In the following example, we have used the seq command in for loop to remove files.
In this article, we have learned how to use the seq command to generate the list of sequential numbers along with it’s different options. The apparently simple seq command when used alone, does not seem very useful. However, when you use seq with it’s options or pipe it to other commands, you realize its true significance.