Home / Alt manpages / seq(1)

  • seq(1)
  • User command
  • linux

Generate Reliable Number Sequences with GNU seq

You will finish with practical commands for generating numbered lines, descending ranges, formatted values and separator-controlled output with GNU seq. The examples use coreutils 9.4, installed here as package version 9.4-3ubuntu6.3. Allow about ten minutes to try them; you need a shell and an ordinary user account. No example needs elevated privileges.

seq writes to standard output. That makes it useful in pipelines and shell loops, but it also means that a careless redirection can overwrite a file. Keep the destination explicit and test the generated output before feeding it to another command.

1. Check the installed command

Confirm which executable your shell will run and record its version. These are read-only checks:

$ command -v seq
/usr/bin/seq
$ seq --version | head -n 1
seq (GNU coreutils) 9.4

The local manual page is dated August 2026 and describes three operand forms: LAST, FIRST LAST, and FIRST INCREMENT LAST. The command treats all three numeric operands as floating-point values, even when they look like integers.

Checkpoint: if command -v seq points somewhere unexpected, stop and inspect your PATH before comparing results with this guide.

2. Generate the basic range

With one operand, seq starts at 1 and counts by 1 up to the supplied last value:

$ seq 5
1
2
3
4
5

With two operands, the first is the starting value:

$ seq 3 7
3
4
5
6
7

The default separator is a newline, and the output ends with a newline. That is normally what you want for line-oriented tools such as while read, grep or xargs.

3. Choose the increment explicitly

Use three operands when the step matters. A negative increment produces a descending range:

$ seq 5 -2 1
5
3
1

Do not expect seq 5 1 to infer a negative step. An omitted increment is always 1, even when the first value is greater than the last, so that command produces no output and still succeeds. Make the direction visible in scripts.

The sequence stops when adding the increment would pass the last value. It does not force the last operand into the output. For example, seq 1 2 4 prints 1 and 3 because the next value, 5, is beyond 4.

Checkpoint: verify the shape before using a range in a loop:

$ test "$(seq 5 -2 1)" = "5
3
1" && echo "range is correct"
range is correct

4. Control separators and padding

-s or --separator places a string between values. The command still terminates the complete output with a newline:

$ seq -s ', ' 1 3
1, 2, 3

Use -w or --equal-width when fixed-width decimal values are more useful than bare numbers. It pads with leading zeroes:

$ seq -w 8 10
08
09
10

For a label or a different numeric presentation, use the printf-style format option. The format must contain a suitable floating-point conversion:

$ seq -f 'item-%02g' 1 3
item-01
item-02
item-03

In shell scripts, quote the format and separator. Unquoted spaces, wildcard characters or shell metacharacters can be interpreted by the shell before seq receives them.

5. Use seq in a pipeline without creating a large argument list

For line-oriented processing, pipe the output directly to the next program:

$ seq 1 5 | while IFS= read -r number; do
>     printf 'number=%s
' "$number"
> done
number=1
number=2
number=3
number=4
number=5

This keeps each value as a line and avoids relying on command substitution. A command substitution such as $(seq 1 5) removes trailing newlines and then subjects the result to word splitting when used unquoted. That can be convenient for a small, trusted list, but it is a poor default for data that may contain spaces or for ranges large enough to stress an argument list.

If a downstream command expects arguments rather than lines, use xargs with care and test the boundary conditions first. For very long sequences, streaming through a pipeline is easier to reason about than expanding the entire sequence into one command invocation.

6. Treat floating-point ranges as approximate

GNU seq accepts decimal and scientific notation, and its output format is inferred when you do not provide -f:

$ seq 0.5 0.25 1
0.50
0.75
1.00

The arithmetic is floating-point. Very small increments, very large integers and values near a representation boundary can produce repeated, skipped or rounded values. If the exact value of every item matters, use an integer representation where possible and convert at the boundary, or use a tool designed for exact decimal or arbitrary-precision arithmetic.

There is also no useful infinite loop protection in a command such as seq inf. The upstream GNU documentation describes it as an infinite sequence. Do not run an unbounded sequence in a terminal, service or unattended job unless the consumer and a separate stop condition are deliberate. To stop a foreground test, press Ctrl+C; a background process may need to be identified and terminated with your normal process-management procedure.

7. Handle errors and file output safely

The increment must not be zero, and the operands must not be NaN. A bad range is not a reason to add sudo:

$ seq 1 0 3
seq reports an invalid zero increment
$ printf 'exit status: %s
' "$?"
exit status: 1

The exact diagnostic can vary with locale, but a non-zero exit status indicates failure. Check it immediately if a pipeline must not continue after an invalid range. In a longer pipeline, consider shell error handling rather than assuming the final command's status describes every earlier command.

Warning: shell redirection with > truncates an existing destination before seq runs. For a new file, choose a new name and verify it:

$ seq -w 1 3 > numbers.txt
$ test -s numbers.txt && cat numbers.txt
01
02
03

For a file that already matters, write to a temporary file in the same directory, inspect it, then replace the destination only when you are satisfied. A failed command can leave an empty or partial redirected file. No recovery is available for an old file that has already been truncated unless you have a backup or another copy.

Done means

  • You checked that the intended GNU seq is installed and noted its version.
  • You used an explicit negative increment for a descending range.
  • You selected -s, -w or -f for the output shape you actually need.
  • You accounted for floating-point approximation before using decimal or very large ranges.
  • You checked exit status and protected important files from accidental truncation.