Mastering the Linux ‘seq’ Command: Quick Ways to Generate Number Lists
This guide explains how to use the Linux seq command to create numeric sequences, customize start, end, and step values, apply separators, pipe results to calculators like bc, and highlights performance tips and limitations for effective shell scripting.
Basic Usage
One of the simplest ways to generate a list of numbers in Linux is with the seq command, which prints a sequence from 1 up to the given number.
$ seq 5
1
2
3
4
5Custom Start and End
By providing a start and an end, you can begin the sequence at a number other than 1.
$ seq 3 5
3
4
5Specifying Increment
You can define an increment (step) value. For example, to list multiples of 3 up to 18:
$ seq 3 3 18
3
6
9
12
15
18A negative increment generates a descending sequence.
$ seq 18 -3 3
18
15
12
9
6
3Performance
The seq command is very fast; generating one million numbers typically finishes in under ten seconds.
$ time seq 1000000
1
2
3
…
999998
999999
1000000
real 0m9.290s
user 0m0.020s
sys 0m0.899sUsing Separators
With the -s option you can replace the default newline separator with any character, such as a colon:
$ seq -s: 3 3 18
3:6:9:12:15:18Using a space as the separator prints the sequence on a single line.
$ seq -s' ' 3 3 18
3 6 9 12 15 18Feeding to bc for Calculations
By choosing a suitable separator, the output of seq can be piped directly into the arbitrary‑precision calculator bc. For example, multiplying numbers 1 through 5:
$ seq -s* 5 | bc
120Even very large products can be computed quickly:
$ time seq -s* 117 | bc
39699371608087208954019596294986306477904063601683223011297484643104\
22041758630649341780708631240196854767624444057168110272995649603642\
560353748940315749184568295424000000000000000000000000000
real 0m0.003s
user 0m0.004s
sys 0m0.000sLimitations
seqaccepts only a single separator, which can restrict more complex calculations. It also works exclusively with numbers; to generate an alphabetic sequence you should use brace expansion instead.
$ echo {a..g}
a b c d e f gSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
