Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

Bash Sequence Expression (Range) - DesignLinux

Oct 27 2020
designlinux 0 Comments
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.

Related

Tags: bash, loop, terminal

How to Set Up WireGuard VPN on Ubuntu 20.04

Prev Post

How to Install OH-MY-ZSH in Ubuntu 20.04

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 0
  • 259
  • 614,631

DesignLinux.com © All rights reserved

Go to mobile version