seq command in Linux with Examples
Last Updated :
10 Sep, 2024
The 'seq' command in Linux is a powerful utility used to generate a sequence of numbers. It is particularly useful in scenarios where you need to create a list of numbers within loops, such as while, for, or until loops. With 'seq', you can quickly generate numbers from a starting value (FIRST) to an ending value (LAST) with optional increments. Here, we will look into the different ways you can use the 'seq' command and explore its options in more detail.
Syntax
The 'seq'
command can be used in various formats:
seq [OPTION]... LAST
seq [OPTION]... FIRST LAST
seq [OPTION]... FIRST INCREMENT LAST
Each of these formats allows you to control the range and step of the sequence generated by the command.

Common Use Cases and Options of 'seq' Command
1. Generating a Sequence from 1 to LAST:
When only one argument is given then it produces numbers from 1 to 'LAST' in step increment of 1. If the 'LAST' is less than 1, then is produces no output.
Example:
seq 10

2. Generating a Sequence from FIRST to LAST:
When two arguments are given then it produces numbers from 'FIRST' till 'LAST' is step increment of 1. If 'LAST' is less than 'FIRST', then it produces no output.
Example:
seq 3 9

3. Generating a Sequence with a Specific Increment:
When three arguments are given then it produces numbers from 'FIRST' till 'LAST' in step of 'INCREMENT'. If 'LAST' is less than 'FIRST', then it produces no output.
Example:
seq 3 7 30

Advanced Options of 'seq' Command
1. seq -f “FORMAT” FIRST INCREMENT LAST
This command is used to generate sequence in a formatted manner. FIRST and INCREMENT are optional.
Example:

2. seq -s "STRING" FIRST INCREMENT LAST
This command is uses to STRING to separate numbers. By default this value is equal to "\n". FIRST and INCREMENT are optional.
Example:

3. seq -w FIRST INCREMENT LAST
This command is used to equalize width by padding with leading zeroes. FIRST and INCREMENT are optional.
Example:

4. seq --help
It displays help information. Displays a help message with details about all options and usage of the 'seq' command.
Example:

5. seq --version:
It displays version information. Displays the version information of the 'seq' command, which can be useful for troubleshooting compatibility issues.
Example:

Conclusion
The 'seq' command in Linux is a simple and flexible tool used for generating sequences of numbers. It offers various options like formatting the output, setting custom separators, and adding leading zeros to ensure equal width, making it an essential utility for many tasks. By learning how to use 'seq' effectively, you can automate repetitive tasks and improve your scripting abilities, making your workflow smoother and more efficient.
Similar Reads
How to check the routing table in Linux | route Command The IP/kernel routing table acts as a crucial map, determining how network packets are forwarded between different hosts and networks. By utilizing the route command, Linux administrators and users can establish static routes, enabling precise control over network connectivity and optimizing data tr
4 min read
rsync command in Linux with Examples rsync or remote synchronization is a software utility for Unix-Like systems that efficiently sync files and directories between two hosts or machines. One is the source or the local-host from which the files will be synced, the other is the remote-host, on which synchronization will take place. Ther
7 min read
SAR command in Linux to monitor system performance sar (System Activity Report) It can be used to monitor Linux system's resources like CPU usage, Memory utilization, I/O devices consumption, Network monitoring, Disk usage, process and thread allocation, battery performance, Plug and play devices, Processor performance, file system and more. Linux s
9 min read
How to Securely Copy Files in Linux | scp Command Secure file transfer is a crucial part of Linux systems administration. Whether moving sensitive files between local machines or transferring data between servers, or you need to move backup files to a remote server, fetch logs from a hosted machine, or sync directories across multiple systems, scp
10 min read
screen command in Linux with Examples The screen command is an advanced terminal multiplexer that allows you to have multiple sessions within one terminal window. It's like having "tabs" in your Linux terminal â you can open, detach, switch, or resume sessions at any time without losing what you're working on. It's particularly convenie
7 min read
script command in Linux with Examples The 'script' command in Linux is a versatile tool that allows you to record all terminal activities, including inputs and outputs, making it a valuable resource for developers, system administrators, educators, and anyone who needs to document terminal sessions. This command captures everything disp
5 min read
scriptreplay command in Linux with Examples scriptreplay command is used to replay a typescript/terminal_activity stored in the log file that was recorded by the script command. With the help of timing information, the log files are played and the outputs are generated in the terminal with the same speed the original script was recorded. The
3 min read
sdiff command in Linux with Examples sdiff command in Linux is used to compare two files and then writes the results to standard output in a side-by-side format. It displays each line of the two files with a series of spaces between them if the lines are identical. It displays a greater than sign if the line only exists in the file spe
3 min read
Sed Command in Linux/Unix With Examples The SED command (short for Stream Editor) is one of the most powerful tools for text processing in Linux and Unix systems. It's commonly used for tasks like search and replace, text transformation, and stream editing.With SED, you can manipulate text files without opening them in an editor. This mak
8 min read
select command in Linux with examples select command in Linux is used to create a numbered menu from which a user can select an option. If the user enters a valid option then it executes the set of commands written in the select block and then asks again to enter a number, if a wrong option is entered it does nothing. If the user enters
2 min read