bash to lower case

For discussions about programming, and for programming questions and advice


Moderator: Forum moderators

Post Reply
User avatar
wiak
Posts: 3673
Joined: Tue Dec 03, 2019 6:10 am
Location: Packing - big job
Has thanked: 57 times
Been thanked: 1028 times
Contact:

bash to lower case

Post 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...

Code: Select all

echo ${var'}
echo ${var''}

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:

Code: Select all

echo ${var^}
echo ${var^^}

or

Code: Select all

echo ${var~}
echo ${var~~}

GNU bash, version 5.0.17(1)-release

https://www.tinylinux.info/
DOWNLOAD wd_multi for hundreds of 'distros' at your fingertips: viewtopic.php?p=99154#p99154
Αξίζει να μεταφραστεί;

Burunduk
Posts: 245
Joined: Thu Jun 16, 2022 6:16 pm
Has thanked: 6 times
Been thanked: 123 times

Re: bash to lower case

Post by Burunduk »

wiak wrote: Sun Aug 14, 2022 10:50 am

but...

Code: Select all

echo ${var'}

didn't work for me. Maybe copied and pasted it wrong?
GNU bash, version 5.0.17(1)-release

Code: Select all

echo ${var^}
User avatar
wiak
Posts: 3673
Joined: Tue Dec 03, 2019 6:10 am
Location: Packing - big job
Has thanked: 57 times
Been thanked: 1028 times
Contact:

Re: bash to lower case

Post by wiak »

Haha... posts crossed! I was editing as you posted

https://www.tinylinux.info/
DOWNLOAD wd_multi for hundreds of 'distros' at your fingertips: viewtopic.php?p=99154#p99154
Αξίζει να μεταφραστεί;

User avatar
wiak
Posts: 3673
Joined: Tue Dec 03, 2019 6:10 am
Location: Packing - big job
Has thanked: 57 times
Been thanked: 1028 times
Contact:

Re: bash to lower case

Post 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/

https://www.tinylinux.info/
DOWNLOAD wd_multi for hundreds of 'distros' at your fingertips: viewtopic.php?p=99154#p99154
Αξίζει να μεταφραστεί;

User avatar
MochiMoppel
Posts: 1137
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 18 times
Been thanked: 370 times

Re: bash to lower case

Post by MochiMoppel »

wiak wrote: Sun Aug 14, 2022 11:19 am

${RELEASE@u}

Requires bash 4.4 or newer.

Burunduk
Posts: 245
Joined: Thu Jun 16, 2022 6:16 pm
Has thanked: 6 times
Been thanked: 123 times

Re: bash to lower case

Post 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
User avatar
wiak
Posts: 3673
Joined: Tue Dec 03, 2019 6:10 am
Location: Packing - big job
Has thanked: 57 times
Been thanked: 1028 times
Contact:

Re: bash to lower case

Post 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...).

https://www.tinylinux.info/
DOWNLOAD wd_multi for hundreds of 'distros' at your fingertips: viewtopic.php?p=99154#p99154
Αξίζει να μεταφραστεί;

Burunduk
Posts: 245
Joined: Thu Jun 16, 2022 6:16 pm
Has thanked: 6 times
Been thanked: 123 times

Re: bash to lower case

Post 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

User avatar
wiak
Posts: 3673
Joined: Tue Dec 03, 2019 6:10 am
Location: Packing - big job
Has thanked: 57 times
Been thanked: 1028 times
Contact:

Re: bash to lower case

Post 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.

https://www.tinylinux.info/
DOWNLOAD wd_multi for hundreds of 'distros' at your fingertips: viewtopic.php?p=99154#p99154
Αξίζει να μεταφραστεί;

User avatar
misko_2083
Posts: 196
Joined: Wed Dec 09, 2020 11:59 pm
Has thanked: 10 times
Been thanked: 20 times

Re: bash to lower case

Post 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

Do you want to exit the Circus? The Harsh Truth
https://www.youtube.com/watch?v=ZJwQicZHp_c

Post Reply

Return to “Programming”