قالب وردپرس درنا توس
Home / Tips and Tricks / How to use seq Command on Linux

How to use seq Command on Linux

A terminal prompt on a Linux computer.
Fatmawati Achmad Zaenuri / Shutterstock

Linux 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̵

7;s it! However, the key word here is “fast”. In a moment, you’ll see how fast this little command can run.

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.

The 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:

seq 6


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

seq 4 10 in a terminal window.

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

see 6 6 48 in a terminal window.

Counts backwards

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

see 24-6 12 in a terminal window.

Calculates decimals

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

seq 1 0.2 2 in a terminal window.

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

time seq 250,000 in a terminal window.

The results are displayed below the list. Even on our moderately powered test PC, seq is surprisingly fast.

Time result for the command seq 250000 in a terminal window.

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

time seq 250000> numbers.txt in a terminal window.” width=”646″ height=”147″ onload=”pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);” onerror=”this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);”/></p>
<p>The time it takes to complete the list and create the file is now about 1/7 second.</p>
<h2>Use a separator</h2>
<p>A new line character is the default character that appears between each number in a list.  This is why they appear as a vertical list with each number on its own line.  If you need, you can provide another separator.</p>
<p>For example, suppose you need to create a comma-separated 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.</p>
<p>We use <code>-s</code> (separator) option.  The following command produces a comma-separated list:</p>
<pre>seq s, 6 6 36</pre>
<p>This command uses a colon (<code>:</code>) as separator:</p>
<pre>seq -s: 6 6 36</pre>
<p>This command tells you <code>seq</code> to use two hyphens (<code>-</code>) as separator:</p>
<pre>seq -s-- 6 6 36</pre>
<p><img class=

Using Format Strings

The 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

seq -f

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

seq -f

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

seq -w 0 100 1000 in a terminal window.

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

seq -s * 6 in a terminal window.

Create files with seq

The 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 touch and 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:

ls file*

point to $ (seq -f

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”:


  for val in $(seq 5 0.2 6.6); do

  echo "The value is now: $val"


Then we write the following to make our new script executable:

chmod +x loop.sh

chmod + x loop.sh in a terminal window.

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:


./loop.sh in a terminal window.

Remember that 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.

Source link