Example Uses of the Linux 'Seq' Command

Generate lists of numbers with ease

The Linux Seq command.

The Linux command Seq prints a sequence of numbers. It's a seemingly simple command that serves several useful purposes.

It's a basic shell program that's standard in Linux regardless of your distribution.

Synopsis of the 'Seq' Command

After entering a shell prompt (a terminal window, often invoked from a windowed environment with the Ctrl+Alt+T hotkey), execute the program by typing:

$seq [option] first increment last

The optional arguments last, first, and increment specify the last number of the sequence, the first number of the sequence, and the value by which the sequence increments. By default, both first and increment default to 1.

Seq parses the optional number arguments in the following order: last, first, increment, even though the attributes don't appear that way when you input the command. Thus, specifying a single numeric argument stipulates the final number in the sequence; specifying two numbers stipulates the beginning and the ending numbers with a default sequence of 1.

Options for the 'Seq' Command

Seq supports five options:

  • -f, --format=format: Stipulates the floating-point format of the resulting number; options include %e, %f, and %g, with %g assumed as default
  • -s, --separator=string: Uses the specified character (or characters) instead of a default newline character
  • -w: Requires the numbers to appear in equal width — i.e., numbers are left-padded with zeroes to max the length of the largest number in the list
  • --help: Displays the on-screen help text
  • --version: Displays the program version

The number formats follow the printf convention. Thus, %e is a double number in standard decimal form, %f is is a double number in fixed-point notation, and %g is a double number in standard or exponential notation, contextual to the magnitude of the number.

Example Basic Uses of the 'Seq' Command

The Linux Seq command.

To display a series of numbers on the screen between 20 and 50:

$seq 20 50

To display a series of numbers on the screen between one and 10:

$seq 10

To display a series of numbers on the screen between 100 and 120, incremented by 2:

$seq 100 2 120

To store the output of the Seq command, instead of displaying it on the standard output, simply pipe it to a file:

$seq 1 5 100 | cat > numbers.txt

Advanced Uses of the 'Seq' Command

Use the options to customize the output.

To generate a list of numbers to 999 that are formatted as three-digit numbers:

$seq -w 999

To generate a list of 500 numbers to a text file that are separated by commas instead of appearing on new lines:

$seq -s , 500 | cat > numbers.txt

To use a space as a part of a separator, enclose the entire string in double quotes.

To generate a list of numbers in reverse:

$seq 10 -1 1

To generate a list of floating-point numbers between zero and one incremented by 0.1:

$seq 0 0.1 1

To generate a list of 100 comma-separated invoice numbers with the prefix Inv- as part of the output and all output with the same length:

$seq -s ", Inv-" -w 100