seq The command generates lists of numbers in the blink of an eye. But how can this functionality be put into practice? We’ll show you how seq can help you.
The seq command
At first glance, Linux
seq The command seems to be a strange thing. It allows you to generate number sequences quick and ready! However, the keyword here is “quickly”. In a moment, you will see how fast this little command can be executed.
Regardless of how they are generated, how useful is a list of numbers? The
seq The command was added to the 8th edition of Unix in 1985. It has been around ever since, so you must do something worthwhile.
The Unix philosophy is that it is full of small utilities that do one thing and do it well. One of the central principles of this philosophy is to write programs that accept information from other programs. Of course, that also means that these programs have to generate output that can be used as input by other programs.
seq The command comes into play when used with other commands that make use of its output, either through pipelines or command line expansion.
Basic list generation
If you throw
seq with a single number as a command line parameter, it counts from one to that number. Then print 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 starting number and the second will be the ending number, as shown below:
seq 4 10
You can set a step size by including a third number. It is between the start and end numbers. We write the following to ask
seq To create a list of numbers that starts with six, ends with 48, and uses a step of six:
seq 6 6 48
We can also ask
seq to create a list of numbers from highest to lowest. However, to do so, we must take a step that is negative.
The following command produces 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
Counting with decimals
The start, end, and step numbers can also be decimal. If any of the numbers are decimal, the others are also treated as decimals. The following command generates a list of numbers with a step of 0.2:
seq 1 0.2 2
The speed of seq
seq it is incredibly fast; the only bottleneck is the time it takes to type the command in the terminal window. To test your speed, let’s ask for a list of 250,000 numbers.
We write the following, using the
time command to see how long the process takes to complete:
time seq 250000
The results are displayed below the list. Even on our moderately powered test PC,
seq it’s surprisingly fast.
The entire list was created and written to the screen in about 1/3 of a second. If we redirect the list to a file, we can even avoid the overhead of writing to the terminal window.
To do this, we write the following:
time seq 250000 > numbers.txt
The time it takes to complete the list and create the file is now approximately 1/7 of a second.
Using a separator
A newline character is the default character that is displayed between each number in a list. This is why they appear as a vertical list, with each number on its own line. If necessary, you can provide another separator.
For example, suppose you need to create a comma-delimited list, a list divided by a colon, or some other punctuation mark or symbol. The delimiter is actually a string, so you can use more than one character.
We will use the
-s (separator) option. The following command will produce a comma delimited list:
seq s, 6 6 36
This command will use a colon (
:) as separator:
seq -s: 6 6 36
This command says
seq use two hyphens
-) as separator:
seq -s-- 6 6 36
Use format strings
seq the command is also supported Language style C format strings. These allow you to format the output with much more control than simply specifying a separator. To use a format string, use the
-f option (format).
The following command says
seq to use zeros to pad the output to two characters:
seq -f "%02g" 6
We can format the string with any text we like 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 up zero padding
The quickest way to set zero padding is to use the
-w (equal width) option. This says
seq use zeros to fill the numbers so that they are all the same width as the largest number.
The following command counts from 0 to 1000 in steps of 100, and all numbers will be padded with zeros:
seq -w 0 100 1000
The longest number is four characters long, so all narrower numbers are padded with zeros up to that width (even 0 is padded with four zeros).
Pipe sequence in bc
By setting the separator as a math symbol and piping the list into the
bc command, we can evaluate the numbers in that list.
The following command generates a list of numbers separated by asterisks (
*), starting at one and ending with six:
seq -s* 6
If we feed that list on
bc, evaluates the list using the asterisks (
*) as multiplication symbols:
seq -s* 6 | bc
We can also do this with other symbols. The following command uses a plus sign (
+) to create a list in which all the numbers are added:
seq -s+ 5
We wrote the following to channel that into
bc and evaluate the list:
seq -s+ 5 | bc
Create files with seq
touch command update date and time stamps in files. If the file doesn’t exist, tap creates it. We can use command line expansion with
seq to create a collection of files with themed names but differently numbered.
We will create a set of 10 files with the same base name and a different number (file-1.txt, file-2.txt, etc.). We write the following:
touch $(seq -f "file-%g.txt" 1 10)
Then we write the following to verify the files:
Using seq in Bash Loops
We can use
seq in bash scripts to control loops with decimals.
Write the following text in an editor and then save it as “loops.sh”:
#!/bin/bash for val in $(seq 5 0.2 6.6); do echo "The value is now: $val" done
Next, 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. Then we can write the following to see that the decimal loop counter increases with each iteration of the loop:
seq you can also count down; you can use that in loops in the same way.
Nice and simple
One thing about
seq is that there is not much learning curve. Have a refreshingly short
man page, but you can still use it in interesting ways.
Because we often need to quickly create realistic sized test files, 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.