Looping cURLy Braces

No, this article is not about my daughter’s curly hair and that expander thingie she had in her mouth for awhile (that sucked for her, booo). Instead we’re talking about expanding braces in shell scripts so we can expand braces in the script.

What’s a brace? As with grammar, a brace is a type of bracket meant to denote a series of characters. The bash interpreter will then allow for the expansion to include a series, such as 1..100 or 1 to 100 or a..z or a through z, useful in loops where the variable name (num in the below example) when invoked in the loop as $num will expand to be the number in the series in the loop. In the below example, we start with a set command and use -B to expand the curly braces:

set -B
for num in {1..100}; do
echo `curl -s -k 'GET' -H 'header info' 'http://krypted.com/log='$num`
echo 'http://krypted.com/log='$num

Note: In the above example, I did an echo just as a debugging line. I’d remove that before actually running the script (or more to the point automating it to run).

If we hadn’t of used the brace, we’d just be typing a lot more to get a 1 to 100 series to expand (e.g. for (( i = 0; i < 20; i++ ))) You can also use curly braces to do some much cooler expansion than a series of numbers. For example, {a,b{1..100},c,d{1…100},e,f{1…100},g} would do a then b1 through b100 then c, etc. In the above e script, once we’ve enabled brace expansion with that -B for set, we can then simply echo a curl onto the screen that loops through a series of 100 log files, using the $i within the loop.

A simpler example might be to use the following , which uses a series of letters instead:

set -B
for letter in {a..z}
echo $letter

If you actually wanted to learn more about palatal expanders then I’m sorry to bore you with everything else – here’s a better place for learning about that: https://en.wikipedia.org/wiki/Palatal_expansion