seq Command: Generating Sequences of Numbers
What you'll be able to do
- Generate sequences with a start, an end, and a step using `seq`
- Repeat work over a sequence with `for i in $(seq ...)`
- Explain zero padding with `-w` and when to prefer `seq` over `{1..10}`
Prerequisites (read these first)
Going to Type 1 Through 100 by Hand?
seq command builds a sequence like "1, 2, 3, ... 100" in an instant.Words used here (sorted out first)
- Sequence: numbers that follow one another, like 1, 2, 3.
- Argument: a value written after the command. In
seq 5, the5is the argument. - Increment: how much to add each time. "Step" means the same thing.
- Loop: a way to repeat the same work. This article uses
for. - Zero padding: adding leading zeros so the digits line up, turning
1into01. - Variable: a box that holds a value for a while. Here the box
ireceives the numbers one at a time. - ShellAn interactive program that reads the commands you type and runs them.: the program in your terminal that reads what you type and runs the commands. "bash" is one name for it.
What You'll Learn
- How to generate a sequence of numbers (1, 2, 3, ...) with
seq - How to set the start, the end, and the increment (step)
- How to combine it with a
forloop to repeat work - How to pad with zeros using
-w, and change the separator with-s - When to use
seqand when to use bash's{1..10}notation
1. What Is the seq Command?
Conclusion:
seqprints a sequence of numbers, one per line.seq 5generates 1 through 5.
seq 5.$ seq 5
1 2 3 4 5
2. How Do I Change the Starting Number?
Conclusion: Pass two arguments and it means
seq START END.seq 3 7generates 3 through 7.
seq 5 10 means "5 through 10".seq.$ seq 5 10
5 6 7 8 9 10
The argument count changes the meaning
seq END→ 1 to ENDseq START END→ START to ENDseq START STEP END→ set an increment (next section)
3. How Do I Set the Increment (Step)?
Conclusion: Pass three arguments and it means
seq START STEP END.seq 0 2 10generates 0 through 10 in steps of 2.
seq 2 2 10 means "2 through 10 in steps of 2".$ seq 2 2 10
2 4 6 8 10
seq 5 -1 1 counts down "5, 4, 3, 2, 1."$ seq 5 -1 1
5 4 3 2 1
It handles decimals too. seq 1 0.5 3 outputs 1.0, 1.5, 2.0, 2.5, 3.0 in steps of 0.5.
When decimals are involved, the whole numbers are printed with the same number of decimal places.
4. How Do I Use a Sequence in a for Loop?
Conclusion: Write
for i in $(seq 1 5)to feed each generated number into the variableiand repeat. Great for creating numbered files in bulk.
for loop with seq. The $(seq 1 5) part is replaced by what seq printed.1 2 3 4 5. Each value goes into i and the body repeats.$ for i in $(seq 1 5); do
> touch "test${i}.txt"
> done$ ls
test1.txt test2.txt test3.txt test4.txt test5.txt
seq 1 100. A 100-line job is done in one line.4-1. Lina Gets Stuck: Forgetting the $( )
Clear away the files you just made before trying this. That way only the new files show up.
$ rm test1.txt test2.txt test3.txt test4.txt test5.txt $ ls
With ls printing nothing, try it once more.
$ for i in seq 1 5; do
> touch "test${i}.txt"
> done
$ lstest1.txt test5.txt testseq.txt
testseq.txt? And I expected five files, but I got three.$( ) is missing. Without it, the shell reads seq, 1, and 5 as three plain words.seq as a command?i gets seq, then 1, then 5. That gives you testseq.txt, test1.txt, and test5.txt — three files.$( ) means "run the command inside and replace it with the output". Always include it when looping over a sequence.seq is not available in the virtual terminal on this site yet. Try the examples in this article on your own Linux machine or terminal.
You can clean up the files you just made. Always look at the targets with ls before deleting.
$ ls test*.txt $ rm test1.txt test5.txt testseq.txt
Unlike a GUI, rm does not move files to a trash folderA container that organizes files. Same idea as a "folder" on Windows or macOS.. They are gone right away. If that makes you nervous, use rm -i, which asks for confirmation one file at a time.
5. How Do I Align the Digits (Zero Padding)?
Conclusion: Add
-wto pad numbers with leading zeros to the widest value.file01,file02, ...file10keep their digits aligned so the sort order stays correct.
ls, they came out in a weird order: test1, test10, test100, test2...ls compares names as text, one character at a time from the left, so 10 lands before 2.-w (width) and it pads with zeros, like 001, 002, ... 100.$ seq -w 1 10
01 02 03 04 05 06 07 08 09 10
Combine it with file creation like this:
for i in $(seq -w 1 100); do touch "log_${i}.txt"; doneThis creates log_001.txt through log_100.txt with aligned digits.
6. How Do I Change the Separator or Format?
Conclusion: Use
-sto change the separator from newline to a comma, space, etc. Use-fto set a printf-style format.
-s, which stands for separator. -s , joins them with commas.$ seq -s , 1 5
1,2,3,4,5
-s " " gives spaces.-f lets you put text in front of the number, or set the digit width.$ seq -f "page-%02g" 1 3
page-01 page-02 page-03
In -f, %g represents a number. The 02 in %02g means "2 digits, zero-padded".
For zero padding alone, -w from the previous section is simpler. -f is the one that lets you add prefixes and set decimal places.
7. How Is It Different from bash's {1..10}?
Conclusion: bash also has
{1..10}for sequences. For fixed values,{1..10}is faster. When the range comes from a variable, useseq.
echo {1..5} print a sequence. How is that different from seq?{1..5} is bash brace expansion, and it is faster than seq.$ echo {1..5}1 2 3 4 5
$ n=5
$ echo {1..$n}{1..5}
{1..$n} did not become a sequence; it printed the literal text.seq 1 $n honors the variable. That is why seq shines when the loop count comes from one.$ n=5 $ seq 1 $n
1 2 3 4 5
for i in $(seq 1 $n) expands the whole sequence into memory when the number is large.
For tens of thousands to millions of iterations, the arithmetic loop for ((i=1; i<=n; i++)) is more efficient. For the everyday range of tens to thousands, seq is fine.
8. Common Pitfalls and Fixes
Conclusion: Most trouble comes from three things: misreading the argument count, forgetting
$( ), and broken sort order from missing zero padding.
| Symptom | Cause | Fix |
|---|---|---|
| A different range than expected | Misunderstood the argument count | Check the order seq START STEP END |
Loop iterates over seq 1 5 text |
Forgot the $( ) |
Write for i in $(seq 1 5) |
| Files sort in a broken order | No zero padding | Align digits with seq -w |
{1..$n} is not a sequence |
Brace expansion ignores variables | Use seq 1 $n |
| Output is a column, not a row | Default separator is a newline | Change it with -s , or -s " " |
What not to do
- Expanding a huge range all at once with
$(seq ...)and exhausting memory - Creating numbered files without zero padding and struggling with sort order later
9. Mini Exercises: Try It Yourself
Conclusion: Three drills — setting a step, zero padding, and combining with a loop — confirm how
seqworks by hand.
$ mkdir -p ~/seq-practice && cd ~/seq-practice
Task 1: Print numbers going down from 10 toward 1, decreasing by 2.
Show hint 1 (direction)
Write three arguments. You want to go down, so the middle number is negative.
Show hint 2 (command name)
The command is seq. Its arguments go in the order seq START STEP END.
Show answer
$ seq 10 -2 1
10 8 6 4 2
The end value is 1, but the number after 2 would be 0, which is below 1. So it stops at 2.
Task 2: Print 1 through 12 with two-digit zero padding.
Show hint 1 (direction)
There is one option for lining up the digits. It appeared in section 5.
Show hint 2 (command name)
The command is seq. The option that lines up the digits is named after "width".
Show answer
$ seq -w 1 12
01 02 03 04 05 06 07 08 09 10 11 12
The largest number is 12, which has two digits, so the whole set becomes two digits.
Task 3: Create 12 files from report_01.txt to report_12.txt in bulk, with the digits aligned.
Show hint 1 (direction)
Feed the sequence from task 2 into a loop, and combine it with the command that creates files.
Show hint 2 (command name)
You need a for loop and touch. Check section 4 for how a sequence is handed to a loop.
Show answer
$ for i in $(seq -w 1 12); do touch "report_${i}.txt"; done
$ ls report_*.txt | head -n 3report_01.txt report_02.txt report_03.txt
Check how many were created as well.
$ ls report_*.txt | wc -l
12
When you are done practising, you can remove the whole directory. Look at the contents before deleting.
$ cd ~ $ ls ~/seq-practice $ rm -r ~/seq-practice
rm -r deletes a directory together with everything inside it. Check that the pathA string that describes the location of a file or directory. really is ~/seq-practice before you run it.
10. Review
seq changes with the argument count. One is the end, two are start and end, and three put the step in the middle.$( ). And -w is what lines up filenames.seq rather than {1..10}.Today's 3-Line Summary
- The meaning of
seqchanges with the argument count:seq END,seq START END,seq START STEP END - In a loop, include the
$( ). Without it the literal wordseqis iterated over - Use
-wto line up digits, and useseqinstead of{1..10}when the range comes from a variable