Bash Sequence Expression (Range)
Published on
•2 min read

In this article, we will cover the basics of the sequence expression in Bash.
The Bash sequence expression generates a range of integers or characters by defining a start and the end point of the range. It is generally used in combination with for
loops.
Bash Sequence Expression
The sequence expression takes the following form:
{START..END[..INCREMENT]}- The expression begins with an opening brace and ends with a closing brace.
STARTandENDcan be either positive integers or single characters.- The
STARTand theENDvalues are mandatory and separated with two dots.., with no space between them. - The
INCREMENTvalue is optional. If present, it must be separated from theENDvalue with two dots.., with no space between them. When characters are given, the expression is expanded in lexicographic order. - The expression expands to each number or characters between
STARTandEND, including the provided values. - An incorrectly formed expression is left unchanged.
Here’s the expression in action:
echo {0..3}When no INCREMENT is provided the default increment is 1:
0 1 2 3You can also use other characters. The example below prints the alphabet:
echo {a..z}a b c d e f g h i j k l m n o p q r s t u v w x y zIf the START value is greater than END then the expression will create a range that decrements:
for i in {3..0}
do
echo "Number: $i"
doneNumber: 3
Number: 2
Number: 1
Number: 0When an INCREMENT is given, it is used as the step between each generated item:
for i in {0..20..5}
do
echo "Number: $i"
doneEach generated number is greater than the preceding number by 5:
Number: 0
Number: 5
Number: 10
Number: 15
Number: 20When using integers to generate a range, you can add a leading 0 to force each number to have the same length.
To pad generated integers with leading zeros prefix either START and END with a zero:
for i in {00..3}
do
echo "Number: $i"
doneNumber: 00
Number: 01
Number: 02
Number: 03The expression can be prefixed or suffixed with other characters:
echo A{00..3}BA00B A01B A02B A03BIf the expression is not constructed correctly, it is left unchanged:
echo {0..}0..Conclusion
The Bash sequence expression allows you to generate a range of integers or characters.
If you have any questions or feedback, feel free to leave a comment.


