Sequence of Numbers in the Same Line in Linux

In Bash, a sequence of numbers in the same line represents a range of integers or a list of specific integers. These sequence numbers are either in ascending or descending order. They have various applications, such as generating indexes of files or creating loops and many more.

This guide will offer different methods to generate a sequence of numbers in the same line in Bash.

  • Method 1: Using “seq” Command
    • Sequence of Numbers
    • Specifying Separator (Comma) 
    • Sequence of Numbers in Descending Order
  • Method 2: Using “echo” Command
    • Range of Integers
    • List of Specific Integers
    • Specifying the Last Number

Method 1: Using the “seq” Command

In Bash, a sequence of numbers in the same line can be generated using the seq command. Here’s the basic syntax of the seq command:

$ seq -s FIRST LAST

In the above syntax, “FIRST” and “LAST” are the first and last numbers in the sequence. The “-s” option specifies a separator between the numbers.

Example 1: Sequence of Numbers 

To generate a sequence of numbers in the same line, use the -s option to specify a separator between the numbers. For instance, generate a sequence of numbers from 1 to 10 separated by spaces via the following command:

$ seq -s ' ' 1 10

The seq command has generated a sequence of numbers from 1 to 10.

Example 2: Sequence of Numbers by Specifying Separator (Comma)

Users can use any separator, such as a comma, a colon, or a hyphen, by specifying it after the -s option. To generate a sequence of numbers from 5 to 15 separated by commas, execute the below command:

$ seq -s ',' 5 15

The output shows the sequence of numbers from 5 to 15 in the same line.

Example 3: Generating a Sequence of Numbers in Descending Order

To generate a sequence of numbers in descending order from 10 to 1, use the “-1” number between 10 and 1:

$ seq -s ' ' 10 -1 1

The output displays the descending order from 10 to 1 in the terminal.

Method 2: Using the “echo” Command

To print the specified sequence of numbers on the standard output, use the “echo” command. The syntax for the “echo” command is as follows:

$ echo [option(s)] [string(s)]

In the above syntax, the “option(s)” are optional and can modify the behavior of the command, while the “string(s)” parameter(s) specify the text or number to be printed.

Example 1: Range of Integers

To present the range of integers between 1 and 10, use the “echo” command as follows:

$ echo {1..10}

It displays the numbers 1 through 10 in a sequence separated by spaces.

Example 2: List of Specific Integers

To display the list of specific integers, use the “echo” command and specify the values in the list as below:

$ echo {2,4,6,8,10}

The numbers 2, 4, 6, 8, and 10 in the sequence have been displayed in the terminal separated by spaces.

Example 3: Specifying the Last Number

Users can also specify the last number to generate the sequence of numbers. In our case, use the “10” as the last number as seen below:

$ echo $(seq 10)

The output shows that the “seq” command generates a sequence of numbers from 1 to 10.

Conclusion

To generate a sequence of numbers in the same line in Bash, use the “seq” and “echo” commands. Users can also utilize the “echo” command to present the specified sequence of numbers on the standard output. But, the “seq” command with the -s option is a convenient way to generate a sequence of numbers in the same line. 

This guide has covered all aspects of generating the sequence of numbers in the same line in Bash.