LinuxCommandLibrary

seq

Print sequences of numbers

TLDR

Sequence from 1 to 10

$ seq 10
copy

Every 3rd number from 5 to 20
$ seq 5 3 20
copy

Separate the output with a space instead of a newline
$ seq [[-s|--separator]] " " 5 3 20
copy

Format output width to a minimum of 4 digits padding with zeros as necessary
$ seq [[-f|--format]] "%04g" 5 3 20
copy

SYNOPSIS

seq [OPTION]... LAST
seq [OPTION]... FIRST LAST
seq [OPTION]... FIRST INCREMENT LAST

Prints a sequence of numbers from FIRST to LAST, with an increment of INCREMENT. If INCREMENT is omitted, it defaults to 1. If FIRST is omitted, it defaults to 1. If FIRST is greater than LAST, the sequence counts downwards if INCREMENT is negative; otherwise, it produces no output.

PARAMETERS

-f, --format=FORMAT
    Use a printf-style FORMAT for outputting numbers (e.g., %.2f for two decimal places).

-s, --separator=STRING
    Use STRING to separate numbers (default is a newline character).

-t, --terminator=STRING
    Use STRING to terminate the sequence (default is a newline character).

-w, --equal-width
    Pad numbers with leading zeros to ensure all numbers have the same width.

--help
    Display help information and exit.

--version
    Display version information and exit.

DESCRIPTION

The seq command is a command-line utility in Unix-like operating systems that prints a sequence of numbers, one per line by default, to standard output. It's a highly versatile tool, frequently employed in shell scripting to generate numerical iterations for loops, create sequentially named files, or manage numerical ranges for various operations. Users can specify a starting number, an ending number, and an optional increment (step value). The numbers generated can be integers or floating-point values. seq also offers options to customize the output format, such as padding numbers with leading zeros to ensure equal width, or defining custom separators and terminators. Its simplicity and robust functionality make it an indispensable tool for automating tasks that involve numerical progression.

CAVEATS

seq uses floating-point arithmetic for its calculations, which can sometimes lead to precision issues with certain decimal values. For operations requiring arbitrary-precision arithmetic, tools like bc(1) might be more suitable. Its output can also be locale-dependent, affecting the decimal point character. While it can generate large sequences, memory constraints could become a factor for extremely long lists.

USAGE EXAMPLES

Generate numbers from 1 to 5:
seq 5

Generate numbers from 5 to 10:
seq 5 10

Generate numbers from 10 down to 1 with a step of 2:
seq 10 -2 1

Generate numbers with a custom separator:
seq -s ',' 1 3 (outputs 1,2,3)

Generate numbers with equal width:
seq -w 9 11 (outputs 09, 10, 11)

ARGUMENT INTERPRETATION

The interpretation of numerical arguments (FIRST, INCREMENT, LAST) depends on the number of arguments provided:

  • If only one argument is provided, it is taken as LAST.
  • If two arguments are provided, they are taken as FIRST and LAST.
  • If three arguments are provided, they are taken as FIRST, INCREMENT, and LAST respectively.

HISTORY

The seq command is a standard utility included in the GNU Core Utilities, a collection of fundamental tools found in Unix-like operating systems, especially Linux distributions. It was developed as a convenient way to generate numerical sequences directly from the command line, often replacing more verbose shell loop constructs or custom scripts for simple numerical progressions. Its design focuses on simplicity and efficiency for common iteration tasks.

SEE ALSO

printf(1), xargs(1), bc(1), awk(1)

Copied to clipboard