Linux Command Reference ยท Text Processing
Linux seq command
Reference for the Linux seq command: print ranges with a step, equal width padding with -w, custom formats and separators, and loops without brace expansion.
seq prints a sequence of numbers, one per line. It accepts one, two or three arguments: the last number, the first and last, or the first, the step and the last. Numbers may be negative or fractional, and the output can be padded, formatted and separated as you like.
Its main uses are feeding loops (for i in $(seq 1 5)), generating test data, and producing zero-padded file names. For simple integer ranges in Bash, brace expansion ({1..5}) is faster because it needs no process, but it cannot take variables directly. seq can.
seq [OPTION]... LAST seq [OPTION]... FIRST LAST seq [OPTION]... FIRST INCREMENT LAST
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
seq [-f FORMAT] [-s SEP] [-w] [FIRST [INCREMENT]] LAST
| Option | Description |
|---|---|
-s STRING |
Separate numbers with STRING instead of a newline. |
-w |
Pad with leading zeros so all numbers have equal width. |
-f FORMAT |
Use a printf-style floating-point format (%e, %f, %g) for each number. |
Returns: 0 on success, non-zero on invalid arguments.
Edge cases: FIRST and INCREMENT default to 1. If the step has the wrong sign, nothing is printed (seq 5 1 prints no lines); use a negative step to count down. A step of 0 is an error.
Usage notes
- Fractions use the step's precision: with
seq 0 0.25 1the output shows two decimals (0.00,0.25). - Fractions accumulate: seq computes each value from the start and the step in floating point, so
seq 0 0.1 1is safe enough but-f '%.1f'hides tiny errors. Never compare seq results as exact decimals. - Large integers: GNU
seqhandles integers beyond 64 bits exactly when no fractions are involved. -wand-fconflict: use one of them.-somits a trailing separator, and the output still ends with a newline.seq -s' ' 3prints1 2 3.for i in $(seq 1 $n)is safe, but beware thatseqis not in POSIX and macOS/BSD versions differ in options. In Bash you can usefor ((i=1; i<=n; i++)), which needs no external command.- Brace expansion pitfalls:
{1..$n}does not expand a variable, because braces are expanded before variables. See expansion order. - Large ranges are cheap:
seqstreams numbers, soseq 1 1000000000 | headfinishes quickly because of SIGPIPE. - Test data: pair with
shuffor random order orpasteto combine columns.