Printing a Sequence of Letters or Numbers

  • HOME
  • >
  • BASH
  • >
  • Printing a Sequence of Letters or Numbers
Last Updated: 

Iterating over a serie of files or directories is a command task when automating some processes. When the number of object is large, you would not want to write those sequences manually. Let see how Bash can simplify those tasks with Brace Expansions.

There is generally two way to generate a sequence of numbers. You can either use the command line tool seq or in a pure Bash way use curly brackets {} (aka curly braces) for Brace Expansion. The later is the prefered way to do as seq is more here for legacy support. Also, note that seq only work to print a sequence of numbers. You will need to use curly brackets for a sequence of letters.

# Using seq
[root@host ~]$ seq 1 10
1 2 3 4 5 6 7 8 9 10

# Using seq with a step number (aka increment)
[root@host ~]$ seq 0 2 10
0 2 4 6 8 10

# Using Brace Expansion
[root@host ~]$ echo {1..10}
1 2 3 4 5 6 7 8 9 10

# Same as above with a Zero 0 as prefix
[root@host ~]$ echo {01..10}
01 02 03 04 05 06 07 08 09 10

# Using Brace Expansion with a step number (aka increment)
[root@host ~]$ echo {0..10..2}
0 2 4 6 8 10
# Sequence of letters with Brace Expansion
[root@host ~]$ echo {a..g}
a b c d e f g

# Same as above with a step number
[root@host ~]$ echo {a..g..2}
a c e g

Example with mkdir

Of course, just printing sequences is not necessary helpful, below is a simple example on applying the same sequence expansion when using mkdir to build a deep tree of directories.

[root@host ~]$ mkdir -p test/{1..10}/{1..10}
[root@host ~]$ ls test/**/
test/1/:
1  10 2  3  4  5  6  7  8  9

test/10/:
1  10 2  3  4  5  6  7  8  9

test/2/:
1  10 2  3  4  5  6  7  8  9

test/3/:
1  10 2  3  4  5  6  7  8  9

test/4/:
1  10 2  3  4  5  6  7  8  9

test/5/:
1  10 2  3  4  5  6  7  8  9

test/6/:
1  10 2  3  4  5  6  7  8  9

test/7/:
1  10 2  3  4  5  6  7  8  9

test/8/:
1  10 2  3  4  5  6  7  8  9

test/9/:
1  10 2  3  4  5  6  7  8  9

👉 See more advanced examples of Braces Expansion with the post Using Bash Wildcards

Related bash posts that you may like
What is the Bash Null Command?
Learn about the Bash null command, also known as the POSIX shell colon command. This post cover concrete use cases and pitfalls to avoid.
How To Format Date and Time in Linux, macOS, and Bash?
Find out how to manipulate date and time on linux and macOS systems as well as natively in the Bash shell. This post covers all you need to know to format a date from your shell.
How To Use Option as Meta Key in macOS Terminal?
The Meta Key is a modifier key that can be quite helpful to improve your productivity while working in a terminal and bash. This post cover how to enable from the command line the Meta Key in macOS Terminal.