The seq command in Linux: generate number sequences

Introduction

In the Linux world, the ability to quickly generate number sequences is essential for tasks such as creating file names, testing loops or feeding data to other programs. The commandseqoffers a simple and powerful solution to produce numerical lists with only a few key pulsations. In this article we will explore your syntax, your most useful options and several practical examples that you can apply to your scripts and the command line.

Basic syntax

The simplest way to useseqis to specify a final number. For example:

seq 5

This will print the numbers from 1 to 5, each in a separate line. If you want to also define the initial number, it is enough to provide two arguments:

seq 3 8

The result will be sequence 3, 4, 5, 6, 7, 8. Finally, you can add a third argument that represents the increase (or step) between values:

seq 0 2 10

This line generates 0, 2, 4, 6, 8, 10. Note that the last number does not exceed the established limit.

Useful options

  • -f or -format: Allows to apply a printf format to each number. For example,seq -f '%02g' 1 9produces 01, 02,... 09.
  • -s or -separator: Change the separator between numbers. By default is the line jump; withseq -s ', ' 1 5You get 1, 2, 3, 4, 5.
  • -w or -equal-width: Add zeros to the left so that all numbers have the same width, useful when alignment is needed.
  • -helpand-version: Show the help and the command version.

Practical examples

Below are some scenarios whereseqis particularly useful.

Create file names with numbering

Suppose you need to generate ten trial files calledfile01.txtafile10.txt. You can combineseqwith a loopforfrom Bash:

for i in $(seq -f '% 02g' 1 10); do touch file$i.txtdone

Generate a sequence for a numerical loop

If you want to go down from 20 to 5, just specify a negative step:

for i in $(seq 20 -2 5); $idone

Feed to other tools

Many programs accept standard entry. For example, to create a file with numbers from 1 to 100 separated by tabulations:

seq -s $'t '1 100 > numbers. tsv

Create a simple coordinate matrix

You can produce pairs (x, y) using two invocation ofseqandpaste:

seq 0 4 | while read x; do seq 0 2 | while read and; do echo $x,$and donor

Tips and tricks

  • When you need zeros to the left, the combination of-fand a format like%03gis more legible than using-walone.
  • Remember thatseqworks with floating point numbers if you are given decimal arguments; for example,seq 0 0.5 2will produce 0, 0.5, 1, 1.5, 2.
  • In portable scripts, check the availability ofseq(is part of the choreutils package in most distributions).
  • To avoid problems with spaces in the separators, use the ANSI-C quoting syntax$'\\t'as previously shown.

Conclusion

The commandseqis a small but incredibly versatile tool that saves time and reduces the complexity of many daily tasks in Linux. Dominating your syntax and your options will allow you to generate precise numerical sequences, format them according to your needs and easily integrate them into more elaborate scripts. The next time you find yourself needing a list of numbers, remember thatseqIt's only one command away.

This work is under aCreative Commons License Attribution 4.0 International for Francesc Roig francesc @ vivaldi.net.

EnglishenEnglishEnglish