Bash For Loop ranges: Easy Guide
In the Bash shell, for loop offers you the power to iterate through sequences of numbers or characters efficiently.
When it comes to defining sequences, ranges come into play. This tutorial focuses on the capability of Bash ranges to craft powerful loops.
Using curly braces {} for range definition
In Bash, curly braces {}
enable you to define a range of values.The syntax looks like {start..end}
, where start
is the beginning value and end
is the concluding value of the range.
Command:
echo {1..5}
Output:
1 2 3 4 5
Here, Bash expands the range between 1 and 5, inclusive. When the echo
command processes the expansion, it prints the entire range.
Basic Number Sequence: {start..end}
Suppose that you need to generate a sequence of numbers. You can use{start..end}
syntax for this.
Command:
for i in {3..7}; do echo $i; done
Output:
3 4 5 6 7
By utilizing the for loop with the range {3..7}
, Bash iterates over each number between 3 and 7, subsequently echoing each number on a new line.
Specifying Increment Value
By default, Bash increments by 1 when iterating through a range. However, you can specify a different increment value by introducing a third parameter within the curly braces.
Positive Increment
Command:
for i in {0..10..2}; do echo $i; done
Output:
0 2 4 6 8 10
In this case, Bash starts at 0 and concludes at 10, incrementing by 2 in each step.
Negative Increment
Command:
for i in {5..1..-1}; do echo $i; done
Output:
5 4 3 2 1
Initiating at 5 and decrementing by 1, the loop traverses backward until it hits the endpoint, 1.
Complex Increment Patterns
Sometimes, your scripts need more sequences. Mixing positive and negative steps can yield unexpected results, so tread with caution.
Command:
echo {5..10..2}{10..5..-2}
Output:
510 58 56 56 54 52 78 76 74 72 910 98 96 96 94 92
The loop first evaluates the left sequence, then appends values from the right sequence, resulting in a composite output.
Character Ranges
Lowercase Sequence
Characters are also allowed when it comes to ranges.
Command:
echo {a..d}
Output:
a b c d
The shell expands the range between ‘a’ and ‘d’, outputting each character in the sequence.
Uppercase Sequence
Upper-case characters can also be sequenced in the same way.
Command:
echo {A..D}
Output:
A B C D
Mixed Character Sequences
Combining upper and lower-case characters can be quite beneficial for certain tasks.
Command:
echo {a..b}{A..B}
Output:
aA aB bA bB
Bash first processes the range on the left, followed by the range on the right, creating combinations of the values.
Nested Loops with Number Ranges
Nested loops enable you to perform iterations within iterations.
Command:
for i in {1..2}; do for j in {a..b}; do echo "$i$j"; done; done
Output:
1a 1b 2a 2b
Here, the outer loop cycles through the numbers 1 to 2, and for each number, the inner loop processes the range from ‘a’ to ‘b’.
Mixed Ranges: {1..3}{a..c}
Mixing numbers and characters within a single loop can be done like this:
echo {1..3}{a..c}
Output:
1a 1b 1c 2a 2b 2c 3a 3b 3c
This syntax is beneficial for generating combinations of numbers and characters.
Handling Impossible Ranges
Not every range you define will be valid. For instance, if you provide an increment that would never allow the loop to reach its end value, Bash again finds it illogical.
echo {1..10..20}
Output:
1
If a range doesn’t make logical sense, Bash doesn’t expand it.
Mokhtar is the founder of LikeGeeks.com. He is a seasoned technologist and accomplished author, with expertise in Linux system administration and Python development. Since 2010, Mokhtar has built an impressive career, transitioning from system administration to Python development in 2015. His work spans large corporations to freelance clients around the globe. Alongside his technical work, Mokhtar has authored some insightful books in his field. Known for his innovative solutions, meticulous attention to detail, and high-quality work, Mokhtar continually seeks new challenges within the dynamic field of technology.