You can use the seq command to iterate a sequence of numbers on the command line or even in Bash. You can also pipe the output to the other files or programs. We will cover all that in this article.
Working with Seq Command
The seq command comes preinstalled on Linux. Its basic syntax is:
The seq command generates a sequence of numbers. You can define how to generate the numbers.1
1. Working with Seq LAST
When only one argument is given, seq treats it as the LAST. It prints the numbers starting from 1 and increments up to that number. The default increment is by 1.
For instance, to use 14 as our argument, the output will be:
2. Working with Seq FIRST and LAST
You can specify where to start the sequence number by adding two arguments. The first represents the starting value, and the other is the last value to be printed. However, the first argument can’t be greater than the LAST argument.
Let’s print the sequence starting from 3 to 14. The command will be:
3. Working with Seq FIRST INCREMENT LAST
When seq receives three arguments, it treats the first argument as the starting point when the sequence number starts. The second argument is the increment number, and the third argument is the last number.
For instance, to print 3 to 14, incrementing the value by 2, the command will be:
4. Working with Formatted Strings
Seq allows the concatenating strings with sequence numbers using the” %g” option. The string format is similar to the C programming, and you can specify the number of characters. Let’s see some of the few examples.
To add the strings before the sequence number, apply the following command:
The previous command prints the sequence numbers from 1 to 5 and appends the words and zeros before the number.
You can also specify an increment and the starting value.
For instance, to print the same formatted strings starting from 11 and incrementing by 4 up to 25, the command will be:
The output would be as follows:
Seq prints the output each on its line. If you prefer displaying the result on the same line, use the -s flag.
5. Working with Seq -w
By default, the width padding of the output is not equal, especially when working with numbers that don’t have the same number of digits. However, you can append the leading zeros to equalize the width using the -w.
6. Working with Separators
A separator is needed when generating the sequence numbers, especially on the same line. The seq offers the -s flag that lets you define the type of separator to use. We added different separators in the following example:
7. Working with Floating-Point Numbers
When you need to generate a sequence containing the floating values, use the “%f” option and add an increment value. For instance, to add a 0.5 increment, the command will be:
8. Working with Bash Scripts Using Seq
You can use the seq in creating Bash scripts. In our case, we will create a Bash script that generates the sequence numbers from 2 to 10 with a 0.8 increment.
The code for the script will be:
Make the script executable and run it to generate the output.
You can also create a script that creates files starting with a given keyword followed by the generated numbers. In our case, we create a script that creates files named lecture and uses the seq to name them.
Run the script. Note the output and how it creates the different files, as shown in the following:
You can also create multiple files on the terminal without using a script. To make the same files as we did, using the Bash script, but on the terminal instead, the command will be:
9. Piping the Seq Output to a File
You can save the output of the sequence number to a new file using the different options. In our case, we will pipe the output to a new file that gets created when the command runs.
Conclusion
Seq is a prompt Linux command that instantly generates the needed sequence numbers. You’ve now understood how to use the seq to create the sequence numbers in different ways, including using it with Bash scripts. You will enjoy how quick it gets the job done.