Debug woes 3: matching uppercase letters
So, I was going through GNU bash manual: Shell Parameter Expansion and trying out examples to check if I was understanding the features well.
When it came to case conversion, it was a bit confusing to know that you can only use a single character length glob. Here are the examples for lowercase to uppercase conversion that I used:
$ fruit='apple'
# all characters to uppercase
$ echo "${fruit^^}"
APPLE
# convert any character that matches [g-z] to uppercase
$ echo "${fruit^^[g-z]}"
aPPLe
# this won't work since 'sky-' is not a single character
$ c='sky-rose'
$ echo "${c^^*-}"
sky-rose
To convert uppercase to lowercase, you just need to use ,
instead of ^
. Sounds simple right? It really is. But, I got stuck while trying to modify the above examples:
$ fruit='APPLE'
# worked as expected
$ echo "${fruit,,}"
apple
# expected 'ApplE' but got 'APPLE'
# can you spot the mistake?
$ echo "${fruit,,[g-z]}"
APPLE
I usually go through documentation and stackexchange sites when I'm stuck. After going through some threads, I came across this unix.stackexchange example:
$ str="HELLO"
$ printf '%s\n' "${str,,[HEO]}"
heLLo
Okay, I thought, this seems similar to what I wanted. Need to check out if this works on my machine. Before I even finished typing the example, my brain's light bulb turned on. I should have used G-Z
instead of lowercase range.
$ echo "${fruit,,[G-Z]}"
ApplE