Page 1 of 1
bash to lower case
Posted: Sun Aug 14, 2022 10:50 am
by wiak
This was new to me:
Code: Select all
# In Bash 4.0
a=DHCP
echo "${a,,}"
# Output:
dhcp
https://www.codegrepper.com/search.php? ... per%20case
Code: Select all
var=hello #For Bash Version higher than 4.3.33 try these
echo ${var''} #Uppercase whole string
HELLO
echo ${var'} #Uppercase only first char of string
Hello
var2=BYE
echo ${var2,} #Lowercase whole string
bye
echo ${var2,,} #Lowercase only first char of string
bYE
but...
didn't work for me. Maybe copied and pasted it wrong or wrong character(s) after var?
Ah, seems to be wrong on website, should be:
or
GNU bash, version 5.0.17(1)-release
Re: bash to lower case
Posted: Sun Aug 14, 2022 11:12 am
by Burunduk
wiak wrote: Sun Aug 14, 2022 10:50 am
but...
didn't work for me. Maybe copied and pasted it wrong?
GNU bash, version 5.0.17(1)-release
Re: bash to lower case
Posted: Sun Aug 14, 2022 11:17 am
by wiak
Haha... posts crossed! I was editing as you posted
Re: bash to lower case
Posted: Sun Aug 14, 2022 11:19 am
by wiak
But I also came across this for something or other:
${RELEASE@u}
Didn't seem to work whatever it is supposed to do. Any idea?
By the way, I had a reason for my search. The qemu frontend quickemu wasn't working for latest EndeavourOS and I was trying to hack it with the new release name and I needed first letter to be capitalized by the quickget script and that ${RELEASE@u} was in the original quickget script at the place I needed to do it (but that didn't work), but now seem to have it downloading latest iso after using ${RELEASE^} instead.
https://github.com/quickemu-project/quickemu/
Re: bash to lower case
Posted: Sun Aug 14, 2022 11:48 am
by MochiMoppel
wiak wrote: Sun Aug 14, 2022 11:19 am
${RELEASE@u}
Requires bash 4.4 or newer.
Re: bash to lower case
Posted: Sun Aug 14, 2022 12:07 pm
by Burunduk
For some reason it doesn't work.
GNU bash, version 5.0.17(1)-release
bash manual wrote:${parameter@operator}
The expansion is either a transformation of the value of parameter or information about parameter itself, depending on the value of operator. Each operator is a single letter:
U
The expansion is a string that is the value of parameter with lowercase alphabetic characters converted to uppercase.
Code: Select all
#var=word
#echo ${var@U}
bash: ${var@U}: bad substitution
Re: bash to lower case
Posted: Sun Aug 14, 2022 12:25 pm
by wiak
MochiMoppel wrote: Sun Aug 14, 2022 11:48 am
wiak wrote: Sun Aug 14, 2022 11:19 am
${RELEASE@u}
Requires bash 4.4 or newer.
Yes, but I'm using: $ bash --version
GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu)
and it didn't work for me either, whilst ${RELEASE^} did. A bit of a mystery - I must have missed some detail I don't know about.
Burunduk wrote: Sun Aug 14, 2022 12:07 pm
For some reason it doesn't work.
GNU bash, version 5.0.17(1)-release
bash manual wrote:${parameter@operator}
...
Yes, now that you posted that I see what it is supposed to do in:
https://www.gnu.org/software/bash/manual/bash.html
Annoyingly the EndeavourOS iso is for some odd reason downloading at 50 KB/sec... and it is late here. Oh well, I'll try again tomorrow since at least it is downloading now...
Hmmm, tried manually downloading it with straight wget -c and same result. Don't usually have that issue; I note its version has just recently been updated so maybe lots of folk downloading it right now...
EDIT: No. My download speed has collapsed from certain parts of the Internet - okay locally. github and sourceforge downloads all very slow for me (that 50 KB/sec - I usually get tons of MB/sec; hope my ISP isn't penalising me for too many iso downloads anti-fair-use policy whilst developing weedogit.sh the past few days...).
Re: bash to lower case
Posted: Sun Aug 14, 2022 12:34 pm
by Burunduk
This document details the changes between this version, bash-5.1-alpha, and
the previous version, bash-5.0-release.
...
dd. New `U', `u', and `L' parameter transformations to convert to uppercase,
convert first character to uppercase, and convert to lowercase,
respectively.
https://git.savannah.gnu.org/cgit/bash.git/tree/CHANGES
Re: bash to lower case
Posted: Sun Aug 14, 2022 12:41 pm
by wiak
Burunduk wrote: Sun Aug 14, 2022 12:34 pm
This document details the changes between this version, bash-5.1-alpha, and
the previous version, bash-5.0-release.
...
dd. New `U', `u', and `L' parameter transformations to convert to uppercase,
convert first character to uppercase, and convert to lowercase,
respectively.
https://git.savannah.gnu.org/cgit/bash.git/tree/CHANGES
Thanks, informative. I haven't checked any bash manual for ages, but I really should. Having said that it is probably good to use older bash syntax when it still works - for the sake of older distros. Alas, quickget from quickemu uses that newer syntax though - hence my original post.
Re: bash to lower case
Posted: Mon Aug 15, 2022 11:07 am
by misko_2083
Capitalize is undocumented
Code: Select all
declare -c var
var=release
echo ${var}
Release
declare -c string
string=(herbivore OS)
echo "${string[@]}"
Herbivore Os
Toggle is also undocumented:
Code: Select all
var=release
echo ${var~}
Release
echo ${var~~}
RELEASE
string="Herbivore OS"
string=${string,,}
string=${string~}
echo "$string"
Herbivore os
Also characters to lower can be defined
Code: Select all
var2="BYE BYE"
echo ${var2,,[BE]}
bYe bYe
echo ${var2,,[YE]}
Bye Bye
echo ${var2~~[YE]}
Bye Bye