seq the command generates lists of numbers in an instant. But how can this function be used in practice? We show how seq can benefit you.
The Seq command
At first glance Linux
seq command seems to be something strange. It allows you to quickly generate sequences of speech and that̵
No matter how they are generated, how useful is a list of numbers? The
seq The command was added to the eighth edition of Unix in 1985. It has been around ever since, so it must do something worthwhile.
Unix’s philosophy is that it is full of small tools that do one thing and do it well. One of the central principles of this philosophy is to write programs that accept input from other programs. Of course, this also means that these programs must generate outputs that can be used as inputs by other programs.
seq the command comes into its own when used with other commands that use its output, either through tubes or command line extension.
Basic list generation
If you start
seq with a single number as a command line parameter, it is counted from one to that number. It then prints the numbers in the terminal window, one number per line, as shown below:
If you type two numbers on the command line, the first will be the start number and the second will be the end number, as shown below:
seq 4 10
You can set a step size by including a third number. It is located between the start and end numbers. We write the following to ask
seq to create a list of numbers beginning with six, ending with 48 and using a step of six:
seq 6 6 48
We can also ask
seq to create a list of numbers from highest to lowest. To do so, however, we must provide a negative step.
The following command provides a list that counts from 24 to 12 in steps of 6 because we write the step as a negative number:
seq 24 -6 12
Start, end and step numbers can also be decimals. If one of the numbers is decimal, the others are also treated as decimals. The following command generates a list of numbers with step 0.2:
seq 1 0.2 2
The speed of seq
seq goes fast – the only bottleneck is the time it takes to type the command in the terminal window. To test its speed, let’s ask for a list of 250,000 numbers.
We write the following using
time command to see how long the process takes to complete:
time sqe 250000
The results are displayed below the list. Even on our moderately powered test PC,
seq is surprisingly fast.
The entire list was created and written to the screen in about 1/3 second. If we redirect the list to a file, we can even avoid entering the terminal window.
To do this, we write the following:
time seq 250000 > numbers.txt
Using Format Strings
seq the command also supports C-style strings in language style. These allow you to format output with much more control than just specifying a separator. To use a format string, use
-f (format) option.
The following command tells
seq to use zeros to outsource production to two characters:
seq -f "%02g" 6
We can format the string with any text we want and place the number anywhere in the string as follows:
seq -f "Number %02g in a C-like format string" 6
A quick way to set zero padding
The fastest way to set up zero padding is to use
-w (equally wide) option. This tells
seq to use zeros to enter the numbers so that they all have the same width as the largest number.
The following command is counted from 0 to 1000 in steps of 100, and all digits are filled with zeros:
seq -w 0 100 1000
The longest number takes four characters, so all narrower numbers are padded with zeros to that width (even 0 is padded to four zeros).
Piping seq Into bc
By setting the separator as a mathematical symbol and leading the list to
bc command, we can evaluate the numbers in the list.
The following command generates a list of numbers separated by asterisks (
*), starts with one and ends with six:
seq -s* 6
If we feed the list to
bcevaluates that list using the asterisks (
*) as multiplication symbols:
seq -s* 6 | bc
We can also do this with other symbols. The command below uses a plus sign (
+) to create a list where all numbers are added:
seq -s+ 5
We write the following to lead it
bc and evaluate the list:
seq -s+ 5 | bc
Create files with seq
touch the command updates time and date stamps on files. If the file does not exist, touch creates it. We can use command line extension with
seq to create a collection of thematic but differently numbered files.
We create a set of 10 files with the same base name and a different number (file-1.txt, file-2.txt and so on). We write the following:
touch $(seq -f "file-%g.txt" 1 10)
Then we write the following to check the files:
Use seq in Bash Loops
We can use
seq in Bash scripts to check loops with decimals.
Enter the following text in an editor and save it as “loops.sh”:
#!/bin/bash for val in $(seq 5 0.2 6.6); do echo "The value is now: $val" done
Then we write the following to make our new script executable:
chmod +x loop.sh
When we run the script, the loop counter is printed in the terminal window. We can then write the following to see the decimal circuit counter increase with each iteration of the loop:
seq can count backwards as well; you can use it in loops in the same way.
Nice and simple
One thing about
seq it’s not much of a learning curve. It has a refreshing card
man page, but you can still use it in interesting ways.
Because we often need to quickly create test files with realistic sizes, we use
seq with a format string. We then redirect the output to create a file that contains as many lines of dummy data as we want.