![]() |
|
All about {Curly Braces} in Bash - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Computers (https://sickgaming.net/forum-86.html) +--- Forum: Linux, BSD & Unix (https://sickgaming.net/forum-88.html) +--- Thread: All about {Curly Braces} in Bash (/thread-88943.html) |
All about {Curly Braces} in Bash - xSicKxBot - 02-27-2019 All about {Curly Braces} in Bash At this stage of our Bash basics series, it would be hard not to see some crossover between topics. For example, you have already seen a lot of brackets in the examples we have shown over the past several weeks, but the focus has been elsewhere. For the next phase of the series, we’ll take a closer look at brackets, curly, curvy, or straight, how to use them, and what they do depending on where you use them. We will also tackle other ways of enclosing things, like when to use quotes, double-quotes, and backquotes. This week, we’re looking at curly brackets or braces: Code: {}Array Builder You have already encountered curly brackets before in The Meaning of Dot. There, the focus was on the use of the dot/period ( Code: .As we saw then: Code: echo {0..10}prints out the numbers from 0 to 10. Using: Code: echo {10..0}prints out the same numbers, but in reverse order. And, Code: echo {10..0..2}prints every second number, starting with 10 and making its way backwards to 0. Then, Code: echo {z..a..2}prints every second letter, starting with z and working its way backwards until a. And so on and so forth. Another thing you can do is combine two or more sequences: Code: echo {a..z}{a..z}This prints out all the two letter combinations of the alphabet, from aa to zz. Is this useful? Well, actually it is. You see, arrays in Bash are defined by putting elements between parenthesis Code: ()Code: month=("Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec")To access an element within the array, you use its index within brackets Code: []Code: $ echo ${month[3]} # Array indexes start at [0], so [3] points to the fourth item AprYou can accept all those brackets, parentheses, and braces on faith for a moment. We’ll talk about them presently. Notice that, all things being equal, you can create an array with something like this: Code: letter_combos=({a..z}{a..z})and Code: letter_combosYou can also do this: Code: dec2bin=({0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1})This last one is particularly interesting because Code: dec2binCode: $ echo ${dec2bin[25]} 00011001Yes, there are better ways of converting decimal to binary as we saw in the article where we discussed & as a logical operator, but it is still interesting, right? Parameter expansion Getting back to Code: echo ${month[3]}Here the braces Code: {}In this case, Code: monthCode: month=("Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec")And, item 3 within the array points to Code: "Apr"Code: [0]Code: echo ${month[3]}Code: echo "Apr"Interpreting a variable as its value is one way of expanding it, but there are a few more you can leverage. You can use parameter expansion to manipulate what you read from variable, say, by cutting a chunk off the end. Suppose you have a variable like: Code: a="Too longgg"The command: Code: echo ${a%gg}chops off the last two gs and prints “Too long“. Breaking this down,
This can be useful for converting files from one format to another. Allow me to explain with a slight digression: ImageMagick is a set of command line tools that lets you manipulate and modify images. One of its most useful tools ImageMagick comes with is Code: convertCode: convertThe following command takes a JPEG image called image.jpg and creates a PNG copy called image.png: Code: convert image.jpg image.pngImageMagick is often pre-installed on most Linux distros. If you can’t find it, look for it in your distro’s software manager. Okay, end of digression. On to the example: With variable expansion, you can do the same as shown above like this: Code: i=image.jpg convert $i ${i%jpg}pngWhat you are doing here is chopping off the extension Code: jpgCode: iCode: pngCode: convert image.jpg image.pngYou may be wondering how this is more useful than just writing in the name of the file. Well, when you have a directory containing hundreds of JPEG images, you need to convert to PNG, run the following in it: Code: for i in *.jpg; do convert $i ${i%jpg}png; done… and, hey presto! All the pictures get converted automatically. If you need to chop off a chunk from the beginning of a variable, instead of Code: %Code: #Code: $ a="Hello World!" $ echo Goodbye${a#Hello} Goodbye World!There’s quite a bit more to parameter expansion, but a lot of it makes sense only when you are writing scripts. We’ll explore more on that topic later in this series. Output Grouping Meanwhile, let’s finish up with something simple: you can also use Code: { ... }Code: echo "I found all these PNGs:"; find . -iname "*.png"; echo "Within this bunch of files:"; ls > PNGs.txtwill execute all the commands but will only copy into the PNGs.txt file the output from the last Code: lsCode: { echo "I found all these PNGs:"; find . -iname "*.png"; echo "Within this bunch of files:"; ls; } > PNGs.txtcreates the file PNGs.txt with everything, starting with the line “I found all these PNGs:“, then the list of PNG files returned by Code: findNotice that there is space between the braces and the commands enclosed within them. That’s because Code: {Code: }Also notice that the list of commands has to end with a semicolon ( Code: ;Next Time In our next installment, we’ll be looking at more things that enclose other things, but of different shapes. Until then, have fun! Read more: And, Ampersand, and & in Linux Ampersands and File Descriptors in Bash Logical & in Bash |