seq

text processingLinux/Unix
The seq command is one of the most frequently used commands in Linux/Unix-like operating systems. seq Print a sequence of numbers

Quick Reference

Command Name:

seq

Category:

text processing

Platform:

Linux/Unix

Basic Usage:

seq [options] [arguments]

Common Use Cases

    Syntax

    seq [options] [first [increment]] last

    Options

    Option Description
    -f, --format=FORMAT Use printf style floating-point FORMAT
    -s, --separator=STRING Use STRING to separate numbers (default: \n)
    -w, --equal-width Equalize width by padding with leading zeros
    --help Display help information and exit
    --version Output version information and exit

    Examples

    How to Use These Examples

    The examples below show common ways to use the seq command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    seq 5
    Print numbers from 1 to 5.
    seq 1 5
    Print numbers from 1 to 5 (same as above).
    seq 2 2 10
    Print even numbers from 2 to 10 with increment 2. # Advanced Examples Advanced seq -w 1 10 Print numbers from 1 to 10 with equal width (leading zeros). seq -f "Number %g" 1 5 Format the output with a prefix. seq -s ", " 1 5 Use comma and space as separator instead of newline. seq -f "%03g" 1 10 Print numbers with leading zeros (001 through 010). seq 10 -1 1 Print numbers in descending order from 10 to 1. seq 0.5 0.5 5 Print numbers with decimal increments. for i in $(seq 1 5); do echo "Processing item $i"; done Use seq in a for loop to iterate through a range of numbers. seq -f "file%03g.txt" 1 10 | xargs touch Create 10 numbered files with leading zeros. seq -s+ 1 10 | bc Calculate the sum of numbers from 1 to 10. seq -f "%g KB" 1024 1024 10240 Create a list of sizes with labels.

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The `seq` command is a versatile utility in Unix-like operating systems that generates a sequence of numbers. It's an essential tool for shell scripting and command-line operations that require iteration over a range of numbers or the generation of numerical sequences. At its most basic, `seq` simply prints numbers from 1 up to the specified last value, with each number on a new line. However, it offers much more flexibility through its various options and parameters, allowing for customized increments, number formatting, and output separators. Key features of the `seq` command include: 1. Flexible Range Definition: You can specify a starting value, an increment, and an ending value to define precisely what sequence you need. 2. Decimal Support: Unlike some counting constructs in shells, `seq` naturally handles decimal values for start, increment, and end parameters. 3. Descending Sequences: By using a negative increment, you can generate sequences that count down rather than up. 4. Output Formatting: The `-f` option provides printf-style formatting capabilities, allowing for custom formatting of each number. 5. Consistent Width Output: The `-w` option ensures all numbers are padded with leading zeros to maintain consistent width, which is particularly useful for generating filenames or other identifiers. 6. Custom Separators: Instead of placing each number on a new line, you can specify an alternative separator using the `-s` option. Common use cases for the `seq` command include: - Generating loop counters in shell scripts - Creating sequences for batch file creation or processing - Generating test data with predictable patterns - Building comma-separated or otherwise delimited lists of numbers - Creating numeric inputs for other commands - Performing simple calculations when combined with tools like `bc` While `seq` is a powerful command on its own, it becomes even more useful when combined with other commands through pipes and command substitution. For example, it's often used with `for` loops, `xargs`, or as input to commands that process multiple items. It's worth noting that some modern shells, like Bash, offer built-in alternatives to `seq` through brace expansion (e.g., `{1..5}`). However, `seq` remains valuable for its more advanced formatting options and broader compatibility across different Unix-like systems.

    Related Commands

    These commands are frequently used alongside seq or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the seq command works in different scenarios.

    $ seq
    View All Commands