Fundamentals 4 min read

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.

ITPUB
ITPUB
ITPUB
Mastering the Linux ‘seq’ Command: Quick Ways to Generate Number Lists

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
5

Custom 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
5

Specifying 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
18

A negative increment generates a descending sequence.

$ seq 18 -3 3
18
15
12
9
6
3

Performance

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.899s

Using 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:18

Using a space as the separator prints the sequence on a single line.

$ seq -s' ' 3 3 18
3 6 9 12 15 18

Feeding 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
120

Even very large products can be computed quickly:

$ time seq -s* 117 | bc
39699371608087208954019596294986306477904063601683223011297484643104\
22041758630649341780708631240196854767624444057168110272995649603642\
560353748940315749184568295424000000000000000000000000000
real    0m0.003s
user    0m0.004s
sys     0m0.000s

Limitations

seq

accepts 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 g
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxCommand-lineshell scriptingnumber sequenceseq
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.