Member-only story
A comprehensive guide to use range in bash
3 min readAug 22, 2024
Two ways we can create a range of values in Bash
- Using Brace
{}
Expansion. - Using
seq
method.
1. Using Brace Expansion
Brace expansion is a powerful feature in Bash that allows you to generate sequences of numbers or characters. It’s the simplest way to create a range.
Syntax:
{start..end}
This syntax generates a sequence starting from the start
value and ending at the end
value. Both the start
and end
will be included
Example : Generating numbers
echo {1..5} # Outputs: 1 2 3 4 5
- Explanation: The brace expansion
{1..5}
tells Bash to create a sequence starting from 1 and ending at 5, generating the numbers: 1, 2, 3, 4, 5.
Example : Generating alphabets
echo {a..e} # Outputs: a b c d e
- Explanation: Similarly,
{a..e}
generates a sequence of characters froma
toe
, so the output will be: a, b, c, d, e.
Specifying a Step Value
You can specify a step value by adding it after the end value, which allows you to control how much the value increments (or decrements) at each step.