LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Slackware (http://www.linuxquestions.org/questions/slackware-14/)
-   -   Broken ${var~} expansion on Bash 4.3.11 ? (http://www.linuxquestions.org/questions/slackware-14/broken-%24%7Bvar%7E%7D-expansion-on-bash-4-3-11-a-4175503115/)

Paulo2 04-27-2014 01:41 AM

Broken ${var~} expansion on Bash 4.3.11 ?
 
Hello friends, if I'm not mistaken, that expansion doesn't work anymore.
Is it intentional, maybe?
I searched with Google but didn't find anything related.

$ echo $BASH_VERSION
4.2.45(2)-release
$ a='the rolling stones'
$ echo ${a~}
The Rolling Stones

$ echo $BASH_VERSION
4.3.11(1)-release
$ a='the rolling stones'
$ echo ${a~}
The rolling stones

Didier Spaier 04-27-2014 02:09 AM

Well, this case modification however provided is not listed among the parameter expansions in the manual for 4.2.37(2)-release that I'm using in Slackware 14.0, so that change is maybe just to make sure the features be in sync with the manual...

I tend to avoid these kinds of bashism anyway ;)

Didier Spaier 04-27-2014 09:07 AM

Let's be a little more constructive:
Code:

bash-4.2$ a='the rolling stones'
bash-4.2$ for i in $a;do echo -n "${i^} ";done;echo
The Rolling Stones
bash-4.2$

Removing the last white space is left to the reader.

fskmh 04-27-2014 10:15 AM

@Didier: There are a bunch of useful parameter expansions in Bash 4 that allow one to do conversions like that directly on a script variable, and I think this is what Paulo is really keen on. (Old farts like us will just have to grudgingly learn them. :-p)

@Paulo2: I can confirm the results you are getting. I have tried recompiling bash 4.3.11 after upgrading to readline-6.3 (in case that was somehow involved) and I have tried compiling it with --enable-casemod-attributes and --enable-casemod-expansions explicitly enabled but none of that helped. Setting bash 4.2 compatibility mode with "shopt -s compat42" doesn't seem to help either.
I have come to the conclusion that this is some kind of regression that should be reported to upstream.

Paulo2 04-27-2014 11:00 AM

@Didier Spaier Yes, that expansion can do the same with little extra work :)

But ${var~} expanded with non blank separators too
bash 4.2.45(2)-release
$ a='the_rolling_stones'
$ echo ${a~}
The_Rolling_Stones

$ a='the.rolling.stones'
$ echo ${a~}
The.Rolling.Stones

$ a='the-rolling-stones'
$ echo ${a~}
The-Rolling-Stones


bash 4.3.11(1)-release
a='the_rolling_stones'
[[ "$a" =~ [^[:alnum:]] ]]
for var in $(tr $BASH_REMATCH ' ' <<<"$a");do
echo -n "${var^}$BASH_REMATCH"
done|sed 's/.$//';echo
The_Rolling_Stones

Will be more complicated with different separators I guess :banghead:


I think Bash is taking the job with little editions, like
those with capitalization, or substitution and trimming,
things done with sed, cut, tr, etc...
Let's see on future releases what they got on 'testing' dir :)

One can use those expansions at home, but if the script
will be public, chances of incompatibility are very big.

@fskmh thanks for the testings, I didn't remember to test all those options.
Is that regression intentional? It will break someone's scripts around the world,
this is for sure.

Paulo2 04-27-2014 11:16 AM

Reading that page parameter expansions there's this
expansion with arrays, it expand all the elements

Quote:

Case modification: Arrays

For array expansion, the case modification applies to every expanded element, no matter if you expand an individual index or mass-expand the whole array using @ or * subscripts. Some examples:

Assume: array=(This is some Text)

echo "${array[@],}"
⇒ this is some text
echo "${array[@],,}"
⇒ this is some text
echo "${array[@]^}"
⇒ This Is Some Text
echo "${array[@]^^}"
⇒ THIS IS SOME TEXT
echo "${array[2]^^}"
⇒ TEXT

ps- Is the last example wrong? :scratch:

fskmh 04-27-2014 11:20 AM

Quote:

Originally Posted by Paulo2 (Post 5160100)
@fskmh thanks for the testings, I didn't remember to test all those options.
Is that regression intentional? It will break someone's scripts around the world,
this is for sure.

That is a question that is best directed at the Bash devs IMO.

One reason why I suspect a regression is that the reverse-case-for-all expansion still works:
Code:

echo $BASH_VERSION; a='the rolling stones'; echo ${a~}; echo ${a~~}
4.3.11(1)-release
The rolling stones
THE ROLLING STONES

This might help you in the meantime though:
Code:

echo $BASH_VERSION; unset a; a=(the rolling stones); echo ${a[@]^}
4.3.11(1)-release
The Rolling Stones


fskmh 04-27-2014 11:33 AM

Quote:

Originally Posted by Paulo2 (Post 5160109)
ps- Is the last example wrong? :scratch:

I think ${BLAH[@]^^} does the same for an array as ${BLAH~~} does for a regular variable.

It seems a=(blah) is the same as declaring an array with "declare -a".
Code:

echo $BASH_VERSION; unset a; a=(the rolling stones); echo ${a~}; echo ${a[@]~}; echo ${a[@]^}
4.3.11(1)-release
The
The Rolling Stones
The Rolling Stones


Paulo2 04-27-2014 03:24 PM

Quote:

This might help you in the meantime though:
Code:

echo $BASH_VERSION; unset a; a=(the rolling stones); echo ${a[@]^}
4.3.11(1)-release
The Rolling Stones
I was reading about expanding arrays, very useful.


Quote:

Quote:
Originally Posted by Paulo2 View Post
ps- Is the last example wrong?
I mean that I think the last example is wrong

array=(This is some Text)

echo "${array[2]^^}"
⇒ TEXT

The element with index 2 is 'some'


All times are GMT -5. The time now is 07:03 PM.