The seq command is an abbreviation for sequence and is used to print a sequence of numbers in increasing or decreasing order. In other words, it prints a range of specified values. The numbers can be integers or real numbers with decimal points, or negative numbers. You can also specify the upper or lower limit of the sequence, etc.
In this tutorial, we will explain how to use the seq command and seq common command options, such as printing a sequence of numbers up to the upper limit, printing a specified value between the lower and upper digits, printing a sequence with custom increments, printing a sequence in decreasing or inverted order, printing a sequence with the same number of digits, printing a sequence in a specified format, printing a sequence using a specified separator, and finally we give you a practical application example for your reference.
Print a sequence of numbers up to an upper limit
In its simplest form, you specify an upper limit for the seq and it will print a sequence from 1 to the upper limit.
Here is an example.
Print specified values between the lower and upper limit numbers
You can provide two numbers in ascending order, lower and upper limit values, and seq will print a sequence of numbers from smallest to largest.
Take a look at this example.
Print a sequence with custom increments
So far, the increment in the sequence has been 1. But you can also customize the increment between the lower and upper limits.
The incremental values can be integers or decimals or negative numbers.
Print a sequence in decreasing or inverted order
Another trick is to print a sequence in decreasing or inverted order. Therefore, you must specify a negative incremental value.
What happens when you enter 0.7 as the incremental value? In this case, seq will not exceed the upper limit.
So far, you have not used any of the options of the seq command. Let’s look at and use the seq options.
Print the sequence with the same number of digits
The seq command’s option
w is used to keep the printed numbers residing in the same number of digits. You will notice that when not enough values are printed, seq will be prepended with zeros. Examples are as follows.
Print the sequence in the specified format
You can use the option
f to format the output lines in the specified format. Examples are as follows.
%g for displaying numbers in integer format.
%e displays numbers in exponential format,
%f displays numbers in floating point format.
Print sequences using the specified separator
So far, sequences have been printed vertically. This is because by default, the separator is a newline character. You can use the option
s to specify the separator. An example is shown below.
' apostrophe in the separator is not required, but it helps avoid shell interpretation. If you use semantic characters for the shell such as
$, a wildcard for regular expressions, the shell will interpret the
$ symbol without the
Practical application of the seq command
You may be wondering what the practical use of the seq command is. There are many situations where it can be used. One example I can think of is when you are using a for loop in bash. Instead of specifying the sequence manually in the loop condition, you can use the seq command.
When you run the above bash script, it will loop through the specified sequence and print the values.
You already know how to use the seq command and the basic options. seq command will print a series of values between the ones you specify. It can also start with negative numbers, as well as in real-world applications. If you want more details, you can always use its manual page.