[email protected]Support 24/7 · Billing 09:00 – 00:00LinkedInXFacebook
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 use iostat command in  Linux.
September 15, 2023

How to use iostat command in Linux.

How To Use Iostat Command. The iostat command in Linux is used for monitoring system input/output statistics for devices and partitions. It monitors system input/output by observing the time the devices are active in relation to their average transfer rates. The iostat produce reports may be used…

Read article
How to use iconv command in Linux.
September 15, 2023

How to use iconv command in Linux.

How To Use Iconv Command. iconv command is used to convert some text in one encoding into another encoding. If no input file is provided then it reads from standard input. Similarly, if no output file is given then it writes to standard output. If no from-encoding or to-encoding is provided then…

Read article
How to use getopts command in Linux.
September 15, 2023

How to use getopts command in Linux.

How To Use Getopts Command. Shell scripting is a powerful way to automate tasks and create custom utilities in Unix-like operating systems such as Linux. When writing shell scripts, it’s common to need to process command-line arguments and options provided by users. The getopts command is a…

Read article
Get in Touch

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

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

Get Started →Contact Us
[email protected]Support 24/7 · Billing 09:00 – 00:00