The Seq Linux command with examples

The Linux seq command generates numbers from a given starting number to a given ending number. The command works like conditional statements such as while and for loops.

You can use the seq command to iterate over a sequence of numbers on the command line or even in bash. You can also direct the output to other files or programs. We will cover all of that in this article.

Working with the Seq command

The seq command is preinstalled on Linux. Its basic syntax is:

$seq [option] FIRST INCREMENT LAST

The seq command creates a sequence of numbers. You can define how the numbers are generated.1

1. Working with Seq LAST

If only one argument is given, seq treats it as the LAST. It prints the numbers starting with 1 and increasing up to that number. The default increment is 1.

For example, to use 14 as an argument, the output is:

2. Working with Seq FIRST and LAST

You can specify where the sequence number should start by adding two arguments. The first represents the start value, the other the last value to be printed. However, the first argument cannot be greater than the LAST argument.

Let’s print the sequence starting at 3 through 14. The command is:

3. Working with Seq FIRST INCREMENT LAST

If seq takes three arguments, it treats the first argument as the starting point when the sequence number begins. The second argument is the increment number and the third argument is the last number.

For example, to print 3 through 14 and increment the value by 2, the command is:

4. Working with formatted strings

Seq allows strings to be concatenated with sequence numbers using the %g option. The string format is similar to C programming, and you can specify the number of characters. Let’s look at some of the few examples.

To add the strings before the sequence number, apply the following command:

$seq -f “NUM%02g” 5

The previous command prints the sequence numbers from 1 to 5 and appends the words and zeros in front of the number.

You can also specify an increment and the starting value.

For example, to print the same formatted strings, starting at 11 and incrementing by 4 through 25, the command is:

$seq -f “NUM%02g” 11 4 25

The output would be as follows:

Seq prints the output on its own line. If you prefer to display the result on the same line, use the -s flag.

5. Working with Seq -w

By default, the width of the output is not equal, especially when working with numbers that do not have the same number of digits. However, you can append the leading zeros to balance the width with the -w.

6. Working with delimiters

A delimiter is needed when generating the sequence numbers, especially on the same line. seq provides the -s flag, which allows you to define the type of delimiter to use. In the example below we have added different delimiters:

7. Working with floating point numbers

If you need to generate a sequence containing the floating point values, use the “%f” option and add an increment value. For example, to add an increment of 0.5, the command is:

8. Working with Bash Scripts Using Seq

You can use the seq when creating bash scripts. In our case we will create a bash script that will generate the sequence numbers from 2 to 10 with an increment of 0.8.

The code for the script is:

Make the script executable and run it to generate the output.

You can also create a script that creates files starting with a specific keyword followed by the generated numbers. In our case, we’ll create a script that creates files named “lecture” and that uses seq to name them.

Run the script. Notice the output and how it creates the various files as shown below:

You can also create multiple files on the terminal without using a script. To create the same files as we do with the bash script, but on the terminal instead, the command is:

$ touch $(seq -f “newlecture-%g.txt” 10)

9. Pipe Seq output to a file

You can save the sequence number output to a new file using the various options. In our case, we are directing the output to a new file that will be created when the command is run.

$seq -f “NUM%02g” 11 4 25 | cat > pipefile.txt

Conclusion

Seq is a prompt Linux command that instantly generates the necessary sequence numbers. You now understand how to use seq to create the sequence numbers in a variety of ways, including using it with bash scripts. You’ll enjoy how quickly it gets the job done.

Related Posts