seq

The seq command in Unix-like operating systems generates sequences of numbers, either in increasing or decreasing order. It's useful in shell scripting for iterating over a range of numbers or generating lists.

Basic Syntax

seq [options] [start] [increment] end
  • options: Optional flags that modify the behavior of the seq command.

  • start: The starting number of the sequence (default is 1).

  • increment: The increment between numbers (default is 1).

  • end: The ending number of the sequence.

Examples

Generating a Sequence of Numbers

# Generate numbers from 1 to 5
seq 1 5

Output:

1
2
3
4
5

Specifying Start and End

Output:

Generating Numbers with Increment

Output:

Generating a Sequence in Reverse Order

Output:

Practical Use Cases

Looping with for and seq

Creating Directories

File Operations

Advanced Examples

Using seq in a for Loop

Generating a Fixed-Length Sequence

Output:

Conclusion

The seq command is a handy tool in shell scripting for generating sequences of numbers efficiently. It simplifies tasks such as looping through ranges of numbers, creating directories or files in batches, and performing mathematical operations in scripts. By leveraging seq, you can make your shell scripts more dynamic and versatile.

Last updated