Categories
Miscellaneous

Quite Some Time

It’s been a little while, hasn’t it? Without looking through past posts because I’d rather type and do a quick edit than research—I can’t remember my last post here.

Even with COVID-19, things have been good. I see life evolving towards goals I want to achieve, which is quite a bit better than standard evolution, which doesn’t really have goals other than existence and propagation.

Philosophy aside, I did some fun work in bash this morning. I do most of my daily work on a Windows 10 machine using WSL and remoting to RHEL and AIX servers. I can’t help but learn and experience. I love responding to updates and being in a ready state for knowledge work. My favorite work is resolving technical issues.

Here’s my work from this morning:

Walkthrough of Curly Braces in Bash

I’m running through “All about {Curly Braces} in Bash” from linux.com:

Array builder functionality (echo)

All of the following have to do with how to use these number or string arrays in bash commands.

Commands executed

goose@gkeener-3976:/mnt/c/Users/gkeener$ echo {10..0}{-1..-10}
10-1 10-2 10-3 10-4 10-5 10-6 10-7 10-8 10-9 10-10 9-1 9-2 9-3 9-4 9-5 9-6 9-7 9-8 9-9 9-10 8-1 8-2 8-3 8-4 8-5 8-6 8-7 8-8 8-9 8-10 7-1 7-2 7-3 7-4 7-5 7-6 7-7 7-8 7-9 7-10 6-1 6-2 6-3 6-4 6-5 6-6 6-7 6-8 6-9 6-10 5-1 5-2 5-3 5-4 5-5 5-6 5-7 5-8 5-9 5-10 4-1 4-2 4-3 4-4 4-5 4-6 4-7 4-8 4-9 4-10 3-1 3-2 3-3 3-4 3-5 3-6 3-7 3-8 3-9 3-10 2-1 2-2 2-3 2-4 2-5 2-6 2-7 2-8 2-9 2-10 1-1 1-2 1-3 1-4 1-5 1-6 1-7 1-8 1-9 1-10 0-1 0-2 0-3 0-4 0-5 0-6 0-7 0-8 0-9 0-10
goose@gkeener-3976:/mnt/c/Users/gkeener$ echo {10..0}{(-1)..(-10)}
-bash: syntax error near unexpected token `('
goose@gkeener-3976:/mnt/c/Users/gkeener$ echo {-1..-10}
-1 -2 -3 -4 -5 -6 -7 -8 -9 -10
goose@gkeener-3976:/mnt/c/Users/gkeener$ echo {10..0} {-1..-10}
10 9 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10
goose@gkeener-3976:/mnt/c/Users/gkeener$ echo {{10..0}..{-1..-10}}
{{10..0}..{-1..-10}}
goose@gkeener-3976:/mnt/c/Users/gkeener$ echo {10..0}{-1..-10}
10-1 10-2 10-3 10-4 10-5 10-6 10-7 10-8 10-9 10-10 9-1 9-2 9-3 9-4 9-5 9-6 9-7 9-8 9-9 9-10 8-1 8-2 8-3 8-4 8-5 8-6 8-7 8-8 8-9 8-10 7-1 7-2 7-3 7-4 7-5 7-6 7-7 7-8 7-9 7-10 6-1 6-2 6-3 6-4 6-5 6-6 6-7 6-8 6-9 6-10 5-1 5-2 5-3 5-4 5-5 5-6 5-7 5-8 5-9 5-10 4-1 4-2 4-3 4-4 4-5 4-6 4-7 4-8 4-9 4-10 3-1 3-2 3-3 3-4 3-5 3-6 3-7 3-8 3-9 3-10 2-1 2-2 2-3 2-4 2-5 2-6 2-7 2-8 2-9 2-10 1-1 1-2 1-3 1-4 1-5 1-6 1-7 1-8 1-9 1-10 0-1 0-2 0-3 0-4 0-5 0-6 0-7 0-8 0-9 0-10
goose@gkeener-3976:/mnt/c/Users/gkeener$ echo {10..00}
10 09 08 07 06 05 04 03 02 01 00
goose@gkeener-3976:/mnt/c/Users/gkeener$ echo thing{00..10}
thing00 thing01 thing02 thing03 thing04 thing05 thing06 thing07 thing08 thing09 thing10

Parameter expansion with String Array in bash

v=("mon" "tue" "wed")
echo ${v[2]} #would print "wed", since that's the 
#third item in the array counting from -0-

Printing a bash Array with echo

me@mycompy:/mnt/c/Users/gkeener$ v=({00..10})
me@mycompy:/mnt/c/Users/gkeener$ for i in {0..10} ; do echo ${v[$i]}; done
00
01
02
03
04
05
06
07
08
09
10

Setting, unsetting variable, testing with [[]] /* if statement */

me@mycompy:/mnt/c/Users/gkeener$ v="test"
me@mycompy:/mnt/c/Users/gkeener$ [[ $v ]] && echo "got it!"
got it!
me@mycompy:/mnt/c/Users/gkeener$ unset v
me@mycompy:/mnt/c/Users/gkeener$ [[ $v ]] && echo "got it!"
me@mycompy:/mnt/c/Users/gkeener$ #no output from above (false result)

Removing last elements of (string) Array**

me@mycompy:/mnt/c/Users/gkeener$ a="Too longgg"
me@mycompy:/mnt/c/Users/gkeener$ echo ${a%gg}
Too long

Removing last element indexed into an Array

me@mycompy:/mnt/c/Users/gkeener$ a=({01..10})
me@mycompy:/mnt/c/Users/gkeener$ echo ${a[1]}
02
me@mycompy:/mnt/c/Users/gkeener$ echo ${a[1]%20}
02
me@mycompy:/mnt/c/Users/gkeener$ echo ${a[1]%2}
0

Incidentally, _ indexing beyond the end of the array simply returns nothing _ / null / 0x00 or something like those nonvalues.

Output Grouping

This is the functionality I’ve found so confusing. { and } can be used to group commands. The syntax is:

{ command args; command 2 args; other-commands args; }

Note the spaces after the ‘{‘ and before the ‘}’. Those are critical for the bash interpreter to read the brackets as grouping brackets. If there is no space, it will interpret curly braces as parameter expansion.

(I think the above note about output grouping is enough for me to remember how *not* to screw up my use of curly braces in Bash.)

Leave a Reply

Your email address will not be published. Required fields are marked *