How to Generate Number Sequences in Linux Terminal Using the seq Command

When automating repetitive shell tasks, provisioning test environments, generating dummy data arrays, or orchestrating batch file renames in Linux, script authors frequently require sequential numeric ranges. While modern shells like Bash support brace expansion (e.g. {1..10}), built-in shell expansions cannot handle runtime variable bounds, floating-point decimals, or zero-padded formatting without cumbersome syntax.

The standard GNU coreutil seq (Sequence) outputs sequences of numbers to standard output with complete control over starting numbers, step increments, end limits, numerical separators, and padding widths.

Basic Sequence Generation

The simplest invocation of seq specifies an upper boundary limit, starting from 1 by default:

seq 5

Output:

1
2
3
4
5

To specify both a starting number and an ending limit, pass two arguments:

seq 10 15

This generates numbers from 10 through 15 inclusive.

Configuring Custom Increments and Decrements

When passed three arguments, the middle number acts as the step increment:

seq FIRST INCREMENT LAST
  • Positive Step Increments:
    seq 0 5 25
    Generates: 0, 5, 10, 15, 20, 25.
  • Counting Backwards (Negative Step):
    seq 10 -2 0
    Generates: 10, 8, 6, 4, 2, 0.
  • Floating-Point Fractional Steps:
    seq 1.0 0.5 3.0
    Generates: 1.0, 1.5, 2.0, 2.5, 3.0.

Equalising Width with Leading Zeros (-w Flag)

When creating numbered filenames, sequential integers without padding cause sorting errors in alphabetical directory listings (where file10.txt sorts before file2.txt). The -w flag equalizes width by padding numbers with leading zeros to match the width of the largest number:

seq -w 1 12

Output:

01
02
03
...
10
11
12

Customising Number Separators (-s Flag)

By default, seq separates each number with a standard newline character. You can replace the newline with any custom string or delimiter using the -s option:

  • Comma-Separated Lists:
    seq -s, 1 10
    Outputs: 1,2,3,4,5,6,7,8,9,10.
  • Space-Separated Values:
    seq -s" " 100 105
    Outputs: 100 105 102 103 104 105.

Automating Shell Loops and Batch Operations

Unlike shell brace expansion, seq works seamlessly with shell variables inside loops:

#!/usr/bin/env bash

START=1
END=5

for i in $(seq -w $START $END); do
    mkdir -p "backup_node_$i"
done

This script dynamically parses shell variables and constructs directories named backup_node_1 through backup_node_5 reliably across all POSIX shell environments.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.