The “seq” Command in Linux [With 8 Practical Examples]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

The seq command in Linux is an efficient tool for producing numerical sequences. It provides users with an easy way to define numerical ranges and carry out different numerical operations straight from the command line. The seq command makes repetitive chores, generating file names, and iterating through numerical ranges easier with its straightforward syntax and flexible capabilities.

In this article, I will demonstrate many features of the Linux seq command using real-world examples.

Syntax of “seq” Command

Using the seq command, users have the ability to define the ending point of a sequence, and optionally, the beginning point (1, if not specified) and the step size between consecutive numbers. The syntax of the seq command typically follows the following formats:

seq [Option]... Last_Value 
seq [Option]... [First_Value] Last_Value 
seq [Option]... [First_Value] [Step_Value] Last_Value

Note: The element enclosed by a square bracket in the above syntax is not mandatory.

EXPLANATION
  • Option: Optional arguments to customize the behavior of the seq command.
  • Last_Value: The upper limit (Ending Point) of the sequence.
  • First_Value: The lower limit (Starting Point) of the sequence.
  • Step_Value: Specifies the step size for progressing or regressing through the sequence.

If you want to know the version history of this command or want help with command, run the following commands respectively:

seq --version
seq --help

Lastly, to get all the required information about this command, run the following command:

man seq

Practical Examples of Using “seq” Command in Linux

Below, I’ll present some practical applications and examples utilizing the seq command so that you may obtain a deeper grasp of this issue and how to utilize the seq command in Linux more effectively:

1. Print Sequence of Numbers Up to Upper Limit

To print a sequence of numbers up to any desired limit starting from 1 and with the default step value of 1, just put the limiting value after the seq command and press ENTER. For example, run the following command:

seq 10

Sequence with upper limit using seq command in linux

In the above image, you can see the sequence with a starting value, ending value, and step value of 1, 10, and 1 respectively.

2. Print Sequence of Numbers From Lower Limit to Upper Limit

Now, to print a sequence with a custom starting and ending point but a default step value of 1, write the value of the starting value beside the seq command followed by the ending value and press ENTER. For example, run the following command:

seq 10 20

Sequence with both upper & lower limit

In the above image, you can see the sequence with a starting value, ending value, and step value of 10, 20 and 1 respectively.

3. Print Sequence of Numbers With Custom Step Value

The step value determines the increment or decrement between numbers in the generated sequence. It specifies how the sequence progresses (positive step value) or regresses (negative step value) from the starting value to the ending value. By default, this value is 1 if you do not mention otherwise while using the seq command.

To generate a sequence with custom starting, ending, and step values, just put the starting value beside the seq command, followed by the step value, followed by the ending value and press ENTER. For example, run the following command:

seq 5 5 20

Again, if you want a sequence to be in the decreasing order, use a negative (-) sign before the step value:

seq 20 -5 5

Sequence with upper, lower and step value

In the above image, the 1st sequence has a positive step value of 5 starting from the number 5 and ending in 20. On the other hand, the 2nd sequence has a negative step value of -5 starting from the number 20 and ending in 5.

Note: You can also use floating numbers as the starting, ending, or step values. The seq command would generate the sequence accordingly.

4. Print Sequence & Maintain Constant Width

While generating a sequence using the seq command, you can ensure that each number in the sequence has the same width by padding them with leading zeros. You can do that by using the -w option. For example, run the following command:

seq -w 5 50 145

Maintaining Constant Width

In the above image, the top 2 numbers in the sequence are padded with leading zeros to match the width of the 3rd value.

5. Print Sequence in Specific Format

When using FORMAT (-f option) to print numbers, it must contain exactly one floating point conversion specification in the style of ‘printf’, such as %a’, %e’, %f’, %g’, %A’, %E’, %F’, or %G’. The FORMAT can also include optional flags, width, and precision.

If no FORMAT is specified, the default format is determined based on the characteristics of the numbers being printed. If all numbers are in fixed-point decimal representation, the default format is “%.Pf”, where P is the minimum precision needed to represent the numbers exactly. Otherwise, the default format is “%g”. For example, run the following command:

seq -f "Leap Year %04" 2000 4 2024

Formatting with -f Option

In the above image, the command generates a sequence of years, starting from 2000 to 2024, with an increment of 4, and formats each number as “Leap Year” followed by a 4-digit year, padded with leading zeros if necessary.

6. Using “seq” Command With “-s” Option

By default, in a sequence generated by the seq command, the numbers appear in one column. But, if you want to change that and make the numbers appear in a different view, such as separated by space, comma (,), colon (:), vertical bar (|), etc, use the -s option followed by the desired separator. For example, to use comma (,) as the separator in any sequence, run the following command:

seq -s, 1 2 10

Using String Options to Format Output

In the above image, I have also shown other examples using different separators. To use space as the separator, you need to enclose it with a quotation. Moreover, to use a character that is also a shell operator in Linux as the separator, such as the vertical bar, you need to put a backslash (\) before it to treat it as an argument to the -s option of the seq command.

7. Using “seq” With Other Commands

The seq command smoothly combines with other commands to dynamically generate input sequences for better usefulness. Its adaptability lets it work with commands such as bc, touch, etc to simplify scripting and execute numerical operations.

7.1. Using “seq” With “bc” Calculator

Using the seq command with the bc calculator in Linux allows for generating sequences of numbers and performing mathematical operations on them. By combining seq with bc, you can create complex numeric sequences and apply calculations to each element in the sequence. This combination is particularly useful for scripting tasks and mathematical computations where sequential operations are required. For example, run the following 2 commands:

seq -s* 5 | bc
seq -s+ 1 2 15 | bc

Using seq command in linux with bc command

In the first example, the seq command generates a sequence from 1 to 5, and each number in the sequence is then piped through the bc command to perform multiplication. The result of each multiplication operation is outputted.

In the second example, the seq command generates a sequence starting from 1, ending at 15, with a step value of 2. Each number in this sequence is then piped through the bc command to perform addition operations, and the result of each addition is displayed.

7.2. Using “seq” With “touch” Command

Using the seq command with the touch command in Linux allows for the creation of files with sequential file names simply and efficiently. By combining seq to generate a sequence of numbers and touch to create files, you can quickly generate multiple files with sequential names. This approach is particularly useful for tasks requiring the creation of a large number of files with systematically ordered names. For example, run the following command:

touch $(seq -f "%g-file.txt" 1 2 7)

EXPLANATION

The command utilizes seq to generate a sequence of numbers starting from 1, ending at 7, with a step of 2. The -f "%g-file.txt" option formats each number in the sequence as a filename with the suffix “-file.txt”. The $(…) syntax is used to execute the seq command and provide its output as arguments to the touch command, resulting in the creation of files named “1-file.txt”, “3-file.txt”, “5-file.txt”, and “7-file.txt”.

Using seq command in linux with touch command

In the above image, you can see that 4 files with sequential file names are created inside the working directory. Here, the ls command lists all the contents inside the working directory.

7.3. Using “seq” With “tail” & “column” Commands

You can use as tail, column, in collaboration with the seq command to generate meaningful and useful results. Using the seq command with the tail command allows for navigation through the end of files or output streams by specifying a range of lines to display.

For example, to output the last 2 numbers of the sequence ranging from 1 to 10 with a step value of 1, run the following command:

seq 1 10 | tail -2

On the other hand, combining the seq command with the column command creates structured and organized output by formatting sequences of numbers into columns.

For example, run the following command:

seq 1 10 | column

Using seq command in linux with tail and column

8. Using “seq” Command in Bash “for” Loop

In a Bash for loop, the seq command creates a series of integers that the loop may iterate over. This makes it simple to repeat a process a certain number of times or iterate over a range of values. By combining seq and the for loop, you may run a command or combination of instructions for each element in the created seque nce. This is especially beneficial for activities that need repeating actions or operations across a range of values. Below I will demonstrate some of the important cases where I incorporated the seq command with for loop in Bash:

8.1. Generating Sequential Numbers

The following script demonstrates how to generate consecutive integers starting from a given value and incrementing by a specified step until reaching the provided end value:

#!/bin/bash

# Printing Sequential Numbers
echo "Printing Sequential Numbers:"

for i in $(seq 1 2 9); do
 echo $i
done

EXPLANATION

This Bash script starts by displaying the message “Printing Sequential Numbers.” It then enters a for loop using the seq command, generating a sequence of numbers starting from 1 and incrementing by 2 until it reaches 9. During each iteration, the script echoes the current number, resulting in the printing of numbers from 1 to 9 with a step size of 2.

After writing and saving the script in a file, first, you need to give executable permission to the file using the chmod command and then run the script by calling it inside the terminal:

Executing bash script for sequential numbers

Here, all odd numbers are printed from 1 to 9.

8.2. Array Element

Using the seq command, the script below shows the indices and elements of an array:

#!/bin/bash

flowers=("Rose" "Waterlily" "Orchid" "Sunflower")
for i in $(seq 0 $((${#flowers[@]} - 1))); do
 echo "$i : ${flowers[$i]}"
done

EXPLANATION

This Bash script begins by defining an array named “flowers” containing four elements: “Rose”, “Waterlily”, “Orchid”, and “Sunflower”. It then enters a for loop using the seq command to generate a sequence of numbers starting from 0 to the length of the “flowers” array minus 1. During each iteration, the script echoes the current index followed by the corresponding flower name from the array. This results in printing the index and name of each flower in the array.

Follow the same procedure as before to run the script.

Executing bash script for array element

Here, array elements are echoed using the seq command.

8.3. Combining Sequences

The following script merges two sequences to form a distinctive pattern:

#!/bin/bash

# Sequences Combined
echo "Sequences Combined:"

for i in $(seq 10 10 100; seq 105 20 200); do
 echo $i
done

EXPLANATION

This Bash script starts by displaying the message “Sequences Combined.” It then enters a for loop using the seq command, which combines two sequences. The first sequence generates numbers starting from 10 and incrementing by 10 until it reaches 100.

The second sequence generates numbers starting from 105 and incrementing by 20 until it reaches 200. During each iteration, the script echoes the current number, resulting in the printing of numbers from 10 to 100 followed by numbers from 105 to 200 with specified step sizes for each sequence.

Follow the same procedure as before to run the script.

Executing bash script for combined sequences

8.4. Creating Files & Naming Them Sequentially

The following script creates files and names them with a sequential numbering pattern using the for loop, touch command, and seq -s command in combination. The -s option is used to specify the separator between numbers:

#!/bin/bash

echo "Files Created!"

for i in $(touch file-$( seq -s '.txt file-' 3 ).txt); do
 echo -n "$i; ";
done

EXPLANATION

This Bash script begins by displaying the message “Files Created!”. It then enters a for loop where the touch command is used to create files with sequential names. The seq command generates a sequence of numbers starting from 1 to 3, and each number is concatenated with the string “file-” and “.txt” to form file names.

The touch command then creates these files. Although the script does not explicitly echo the file names in the terminal, it does create the files as specified.

Follow the same procedure as before to run the script. You can check if the files are created or not using the ls command.

Executing bash script for creating files

8.5. Hexadecimal Sequence

The following script utilizes the seq command to generate a sequence of hexadecimal numbers:

#!/bin/bash

echo "Hexadecimal Sequence:"

for i in $( seq  0x9C 0xF 0xCD ); do
 printf "%X; " $((i))
done

echo

EXPLANATION

This Bash script begins by displaying the message “Hexadecimal Sequence:”. It then enters a for loop using the seq command to generate a sequence of hexadecimal numbers starting from 9C, incrementing by F, and continuing until reaching CD.

During each iteration, the script uses printf to convert the current number to hexadecimal format using the %X format specifier and then echoes the result followed by a semicolon and space. This results in the printing of the hexadecimal numbers generated by the sequence.

Follow the same procedure as before to run the script.

Executing bash script for hexadecimal sequence

Here, I generated a hexadecimal sequence. The input parameters such as the starting point, ending point, and step value are also hexadecimal numbers.

Conclusion

In this article, I explored the features of the seq command in Linux and showed you how useful it is for creating numerical sequences for a variety of purposes. To customize the usage of seq command, I urge readers to investigate the capabilities of the seq command and combine it with other command-line tools. Thank you for exploring the “seq” command with us!

People May Ask

What is the purpose of the seq command in Linux?

The seq command is used to generate sequences of numbers. It can be helpful in generating numeric ranges for various scripting and command-line tasks.

How can I generate a sequence of numbers in reverse order using seq?

To generate a sequence in reverse order, you can provide a negative increment value. For example, seq 10 -1 1 will generate a sequence from 10 to 1 in reverse order.

Can I use seq in a shell script?

Yes, seq is commonly used in shell scripts to generate numeric sequences for various purposes, such as looping through a range of values or generating file names.

Can I use floating points as the starting, step, and ending values?

Yes, the seq command in Linux can handle floating-point numbers directly, both for the start, end, and increment values. For example, run the command: seq 1.5 0.5 3.5. This will display the following numbers: 1.5, 2.0, 2.5, 3.0, 3.5.

Rate this post
Avatar photo

Md. Nafis Soumik graduated from the Bangladesh University of Engineering & Technology, Dhaka, with a BSc—Engg in Naval Architecture & Marine Engineering. In January 2023, Soumik joined Softeko as an Excel and VBA content developer. Since then, he has authored over 50 articles, spanning topics from fundamental to advanced Excel concepts such as Data Analysis and Manipulation, Data Visualization, Pivot Tables, Power Query, VBA, and so on. He participated in 2 specialized training programs on VBA and Chart & Dashboard design in Excel. Furthermore, he possesses a keen interest in Linux and currently serves as an executive content developer specializing in Linux. He aims to produce valuable content tailored to Linux users. During his leisure time, he enjoys music, traveling, and science documentaries. Read Full Bio

Leave a Comment