All about Curly Braces in Bash


Get real time updates directly on you device, subscribe now.

At this stage of our Bash fundamentals collection, it might be arduous to not see some crossover between subjects. For instance, you’ve already seen a whole lot of brackets within the examples we have now proven over the previous a number of weeks, however the focus has been elsewhere.

For the subsequent section of the collection, we’ll take a more in-depth take a look at brackets, curly, curvy, or straight, how you can use them, and what they do relying on the place you employ them. We will even sort out different methods of enclosing issues, like when to make use of quotes, double-quotes, and backquotes.

This week, we’re taking a look at curly brackets or braces: .

Array Builder

You may have already encountered curly brackets earlier than in The Which means of Dot. There, the main focus was on using the dot/interval (.), however utilizing braces to construct a sequence was equally vital.

As we noticed then:


prints out the numbers from zero to 10. Utilizing:


prints out the identical numbers, however in reverse order. And,


prints each second quantity, beginning with 10 and making its approach backwards to zero.


echo z..a..2

prints each second letter, beginning with z and dealing its approach backwards till a.

And so forth and so forth.

One other factor you are able to do is mix two or extra sequences:


This prints out all the 2 letter combos of the alphabet, from aa to zz.

Is this convenient? Nicely, really it’s. You see, arrays in Bash are outlined by placing parts between parenthesis () and separating every component utilizing an area, like this:

month=(“Jan” “Feb” “Mar” “Apr” “Might” “Jun” “Jul” “Aug” “Sep” “Oct” “Nov” “Dec”)

To entry a component throughout the array, you employ its index inside brackets []:

$ echo $month[3] # Array indexes begin at [0], so [3] factors to the fourth merchandise


You may settle for all these brackets, parentheses, and braces on religion for a second. We’ll discuss them presently.

Discover that, all issues being equal, you’ll be able to create an array with one thing like this:


and letter_combos factors to an array that comprises all of the 2-letter combos of your complete alphabet.

You can too do that:


This final one is especially attention-grabbing as a result of dec2bin now comprises all of the binary numbers for an Eight-bit register, in ascending order, beginning with 00000000, 00000001, 00000010, and so on., till reaching 11111111. You need to use this to construct your self an Eight-bit decimal-to-binary converter. Say you need to know what 25 is in binary. You are able to do this:

$ echo $


Sure, there are higher methods of changing decimal to binary as we noticed within the article the place we mentioned & as a logical operator, however it’s nonetheless attention-grabbing, proper?

Parameter enlargement

Getting again to

echo $month[3]

Right here the braces are usually not getting used as aside of a sequence builder, however as a approach of producing parameter enlargement. Parameter enlargement includes what it says on the field: it takes the variable or expression throughout the braces and expands it to no matter it represents.

On this case, month is the array we outlined earlier, that’s:

month=(“Jan” “Feb” “Mar” “Apr” “Might” “Jun” “Jul” “Aug” “Sep” “Oct” “Nov” “Dec”)

And, merchandise three throughout the array factors to “Apr” (bear in mind: the primary index in an array in Bash is [0]). That signifies that echo $month[3], after the enlargement, interprets to echo “Apr”.

Deciphering a variable as its worth is a technique of increasing it, however there are just a few extra you’ll be able to leverage. You need to use parameter enlargement to control what you learn from variable, say, by chopping a bit off the top.

Suppose you’ve a variable like:

a=”Too longgg”

The command:

echo $apercentgg

chops off the final two gs and prints “Too lengthy”.

Breaking this down,

$ tells the shell to increase no matter is inside it
a is the variable you might be working with
% tells the shell you need to chop one thing off the top of the expanded variable (“Too longgg”)
and gg is what you need to chop off.

This may be helpful for changing information from one format to a different. Enable me to clarify with a slight digression:

ImageMagick is a set of command line instruments that permits you to manipulate and modify pictures. Considered one of its most helpful instruments ImageMagick comes with is convert. In its easiest type convert permits you to, given a picture in a sure format, make a replica of it in one other format.

The next command takes a JPEG picture known as picture.jpg and creates a PNG copy known as picture.png:

convert picture.jpg picture.png

ImageMagick is commonly pre-installed on most Linux distros. If you cannot discover it, search for it in your distro’s software program supervisor.

Okay, finish of digression. On to the instance:

With variable enlargement, you are able to do the identical as proven above like this:


convert $i $ipercentjpgpng

What you might be doing right here is chopping off the extension jpg from i after which including png, making the command convert picture.jpg picture.png.

Chances are you’ll be questioning how that is extra helpful than simply writing within the identify of the file. Nicely, when you’ve a listing containing lots of of JPEG pictures, it’s essential convert to PNG, run the next in it:

for i in *.jpg; do convert $i $ipercentjpgpng; finished

… and, hey presto! All the images get transformed routinely.

If it’s essential chop off a bit from the start of a variable, as a substitute of %, use #:

$ a=”Howdy World!”

$ echo Goodbye$

Goodbye World!

There’s fairly a bit extra to parameter enlargement, however a whole lot of it is sensible solely when you find yourself writing scripts. We’ll discover extra on that subject later on this collection.

Output Grouping

In the meantime, let’s end up with one thing easy: you may also use to group the output from a number of instructions into one large blob. The command:

echo “I discovered all these PNGs:”; discover . -iname “*.png”; echo “Inside this bunch of information:”; ls > PNGs.txt

will execute all of the instructions however will solely copy into the PNGs.txt file the output from the final ls command within the checklist. Nonetheless, doing

> PNGs.txt

creates the file PNGs.txt with the whole lot, beginning with the road “I discovered all these PNGs:”, then the checklist of PNG information returned by discover, then the road “Inside this bunch of information:” and ending up with the whole checklist of information and directories throughout the present listing.

Discover that there’s house between the braces and the instructions enclosed inside them. That’s as a result of are reserved phrases right here, instructions constructed into the shell. They’d roughly translate to “group the outputs of all these instructions collectively” in plain English.

Additionally discover that the checklist of instructions has to finish with a semicolon (;) or the entire thing will bork.

Subsequent Time

In our subsequent installment, we’ll be taking a look at extra issues that enclose different issues, however of various shapes. Till then, have enjoyable!

Learn extra:

And, Ampersand, and & in Linux

Ampersands and File Descriptors in Bash

Logical & in Bash

Source link

Leave A Reply

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More