[email protected]Office hours: Mon – Fri 9:00AM – 5:00PMLinkedInXFacebook
NexonHost
Netherlands Dedicated ServersAmsterdam · Equinix AM6Germany Dedicated ServersFrankfurt · Equinix FR4Romania Dedicated ServersBucharest · VoxilityAustria Dedicated ServersVienna · Interxion VIEBulgaria Dedicated ServersSofia · TelepointFrance Dedicated ServersParis · Interxion PAR — Marseille · Digital Realty MRS3Ireland Dedicated ServersDublin · Equinix DB2Italy Dedicated ServersMilan · Irideos AvalonPoland Dedicated ServersWarsawSpain Dedicated ServersMadrid · Equinix MD2United States Dedicated ServersAshburn · Equinix DC
All articles
Getting startedJuly 21, 2023

How to use Bash Sequence Expression

How To Use Bash Sequence Expression

Nexonhost Dedicated Servers , Virtual Servers

Bash Sequence Expression

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.

The sequence expression takes the following form:

  • {START..END[..INCREMENT]}
  • The expression begins with an opening brace and ends with a closing brace.
  • START and END 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. If present, it must be separated from the END value 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 START and END, 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 3

You 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 z

If 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”
  • done
  • Number: 3
  • Number: 2
  • Number: 1
  • Number: 0

When an INCREMENT is given, it is used as the step between each generated item:

  • for i in {0..20..5}
  • do
  • echo “Number: $i”
  • done

Each generated number is greater than the preceding number by 5:

  • Number: 0
  • Number: 5
  • Number: 10
  • Number: 15
  • Number: 20

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

The expression can be prefixed or suffixed with other characters:

  • echo A{00..3}B
  • A00B A01B A02B A03B

If 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.

Keep reading
How to Implement Network Segmentation and Resource Isolation in Linux.
February 6, 2024

How to Implement Network Segmentation and Resource Isolation in Linux.

How to Implement Network Segmentation and Resource Isolation in Linux. Securing and managing networks in a Linux environment involves advanced practices like network segmentation and resource isolation. This guide explores these concepts, providing detailed instructions and examples for…

Read article
How to open or close ports in Linux.
February 6, 2024

How to open or close ports in Linux.

How to open or close ports in Linux. Effectively managing ports on your Linux system is crucial for both security and service accessibility. This comprehensive guide will provide a detailed walkthrough on opening and closing ports using the robust tools iptables and ufw. Understanding these…

Read article
How to use ypdomainname command.
January 23, 2024

How to use ypdomainname command.

How to use ypdomainname command. In the realm of Linux networking, the ypdomainname command plays a crucial role in the context of the Network Information Service (NIS). NIS, formerly known as Yellow Pages, is a distributed database service that facilitates the sharing of network configuration and…

Read article
Get in Touch

Together, Let’s Build a Faster, Safer Internet.

Build scalable infrastructure with NexonHost — high-performance dedicated servers, VPS hosting, colocation, and DDoS protection across Europe.

Get Started →Contact Us
[email protected]Office hours: Mon – Fri 9:00AM – 5:00PM