• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

Bash Sequence Expression (Range)

Written by Admin, Updated On October 26, 2020
bash, loop, terminal
Bash while Loop

Using Bash sequence expression you can generates a range of integers or characters by providing the start and the end point of the range. In this guide, we will show you the basics of the sequence expression in Bash.

Bash Sequence Expression#

Below is the basic form of the sequence expression:

{START..END[..INCREMENT]}

The expression starts with an opening brace and ends with a closing brace.

  • Starting and ending can be either positive integers or single characters.
  • The START and the END values are mandatory and separated with two dots .., with no space between them.
  • The INCREMENT value is optional and if provided then it must be separated from the END value with two dots without space in between. If characters are given, the expression is expanded in alphabetic order.
  • The expression expands to each number or characters between START and END, including the provided values.
  • An incorrectly formed expression is left unchanged

Below is the expression in action:

echo {0..5}

If the INCREMENT is not provided the default increment is 1:

0 1 2 3 4 5

To print the alphabet use below:

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 z

When the END value is greater than the START value then the expression will create a range in decrement:

for i in {5..0}
do
  echo "Number: $i"
done
Number: 5
Number: 4
Number: 3
Number: 2
Number: 1
Number: 0

If an INCREMENT is given, it is used as the step between each generated item. Let’s see an example with increment value:

for i in {0..25..5}
do
  echo "Number: $i"
done
Number: 0
Number: 5
Number: 10
Number: 15
Number: 20
Number: 25

When 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"
done
Number: 00
Number: 01
Number: 02
Number: 03

If you want the rang with any prefix or suffix, you can set prefix or suffix with other characters as following:

echo A{00..5}B
A00B A01B A02B A03B A04B A05B

If the expression is not correct, there will be no change:

echo {0..}
0..

Conclusion#

You learned how to generate a range of integers or characters using the Bash sequence expression.

If you have any questions or feedback, feel free to leave a comment.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article Gunzip Command in Linux
Next Article   Bash break and continue

Related Posts

  • How to Install SSH Keys on Ubuntu 22.04

    How to Set up SSH Keys on Ubuntu 22.04

    January 7, 2023
  • How to Install Fail2ban on Ubuntu 22.04

    How to Install and Configure Fail2ban on Ubuntu 22.04

    December 5, 2022
  • How to Enable SSH on Ubuntu 22.04

    How to Enable SSH on Ubuntu 22.04

    December 1, 2022

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook