seq Command: Generating Sequences of Numbers

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?

Lina: Senpai, I need to create test1.txt all the way to test100.txt. Do I really have to type the command 100 times?
Linny-senpai: Hold on, that would be painful. The seq command builds a sequence like "1, 2, 3, ... 100" in an instant.
Linny-senpai: Combine it with a loop and you can create all 100 files in a single line.

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, the 5 is 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 1 into 01.
  • Variable: a box that holds a value for a while. Here the box i receives 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 for loop to repeat work
  • How to pad with zeros using -w, and change the separator with -s
  • When to use seq and when to use bash's {1..10} notation

1. What Is the seq Command?

Conclusion: seq prints a sequence of numbers, one per line. seq 5 generates 1 through 5.

Lina: What does "seq" even stand for?
Linny-senpai: It's short for sequence, meaning a run of values in order.
Linny-senpai: The simplest use is passing a single number. Try typing seq 5.
$ seq 5
1
2
3
4
5
Lina: The numbers 1 to 5 appeared in a column.
Linny-senpai: Right. Pass one number and it prints 1 up to that number.
Linny-senpai: One value per line is the key part. Feed that into a loop and it becomes a repeating task.

2. How Do I Change the Starting Number?

Conclusion: Pass two arguments and it means seq START END. seq 3 7 generates 3 through 7.

Lina: What if I want 5 to 10 instead of starting from 1?
Linny-senpai: Line up two numbers. seq 5 10 means "5 through 10".
Linny-senpai: The number of arguments changes the meaning. That is the neat part of seq.
$ seq 5 10
5
6
7
8
9
10

The argument count changes the meaning

  • seq END → 1 to END
  • seq START END → START to END
  • seq 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 10 generates 0 through 10 in steps of 2.

Lina: I want to print every other number, like "2, 4, 6, 8."
Linny-senpai: Put the increment in the middle. That is how much to add each time.
Linny-senpai: seq 2 2 10 means "2 through 10 in steps of 2".
$ seq 2 2 10
2
4
6
8
10
Lina: Can I count down from a big number to a small one?
Linny-senpai: Yes. Make the increment negative. 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 variable i and repeat. Great for creating numbered files in bulk.

Lina: Finally, the way to create 100 files.
Linny-senpai: We combine a for loop with seq. The $(seq 1 5) part is replaced by what seq printed.
Linny-senpai: So it becomes 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
Lina: Five files appeared at once. So if I change it to 100...
Linny-senpai: Exactly, just use 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.

Lina: Senpai, I thought I did the same thing, but I got a file with a strange name.
$ for i in seq 1 5; do
>   touch "test${i}.txt"
> done
$ ls
test1.txt  test5.txt  testseq.txt
Lina: What is testseq.txt? And I expected five files, but I got three.
Linny-senpai: The $( ) is missing. Without it, the shell reads seq, 1, and 5 as three plain words.
Lina: So it never runs seq as a command?
Linny-senpai: Correct. i gets seq, then 1, then 5. That gives you testseq.txt, test1.txt, and test5.txt — three files.
Lina: The filenames tell the whole story. That makes sense now.
Linny-senpai: $( ) 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 -w to pad numbers with leading zeros to the widest value. file01, file02, ... file10 keep their digits aligned so the sort order stays correct.

Lina: When I listed test1.txt through test100.txt with ls, they came out in a weird order: test1, test10, test100, test2...
Linny-senpai: That's because the digit counts differ. ls compares names as text, one character at a time from the left, so 10 lands before 2.
Linny-senpai: Use -w (width) and it pads with zeros, like 001, 002, ... 100.
Lina: Once the digits line up, sorting as text keeps the order correct.
$ seq -w 1 10
01
02
03
04
05
06
07
08
09
10
Lina: Everything is two digits now.
Linny-senpai: The largest number here is 10, which has two digits. Everything matches that width.
Linny-senpai: Go up to 100 and they align to three digits, from 001 to 100.

Combine it with file creation like this:

for i in $(seq -w 1 100); do touch "log_${i}.txt"; done

This creates log_001.txt through log_100.txt with aligned digits.

6. How Do I Change the Separator or Format?

Conclusion: Use -s to change the separator from newline to a comma, space, etc. Use -f to set a printf-style format.

Lina: What if I want "1,2,3,4,5" in a row instead of a column?
Linny-senpai: Use -s, which stands for separator. -s , joins them with commas.
$ seq -s , 1 5
1,2,3,4,5
Lina: Now it's one line. Can I use a space separator too?
Linny-senpai: -s " " gives spaces.
Linny-senpai: And -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, use seq.

Lina: I once saw echo {1..5} print a sequence. How is that different from seq?
Linny-senpai: Good eye. {1..5} is bash brace expansion, and it is faster than seq.
Linny-senpai: But it has a weakness. It does not expand when you use a variable.
$ echo {1..5}
1 2 3 4 5
$ n=5
$ echo {1..$n}
{1..5}
Lina: Huh, {1..$n} did not become a sequence; it printed the literal text.
Linny-senpai: Right. Brace expansion does not work with variables.
Linny-senpai: But 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 seq works by hand.

Lina: I understand how it works. I want to confirm it hands-on.
Linny-senpai: Good. Here are three tasks. Make a practice directory first and work inside it.
$ 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 3
report_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

Lina: Let me sum up. The meaning of seq changes with the argument count. One is the end, two are start and end, and three put the step in the middle.
Linny-senpai: Exactly. Remember that and you will always get the range you intended.
Lina: In a loop I must include the $( ). And -w is what lines up filenames.
Linny-senpai: Perfect. And when the range comes from a variable, use seq rather than {1..10}.

Today's 3-Line Summary

  1. The meaning of seq changes with the argument count: seq END, seq START END, seq START STEP END
  2. In a loop, include the $( ). Without it the literal word seq is iterated over
  3. Use -w to line up digits, and use seq instead of {1..10} when the range comes from a variable

Summary / Next Reading

Share this article

Next steps