Bash capital–non-capital substitution

W. Scott Lockwood III scott at guppylog.com
Mon Oct 28 15:53:48 UTC 2013


On Mon, 2013-10-28 at 09:49 -0600, Wes James wrote:
> On Sat, Oct 26, 2013 at 6:53 AM, Johnny Rosenberg
> <gurus.knugum at gmail.com> wrote:
>         
>         x="hello"
>         echo ${x} → hello
>         echo ${x^} → Hello
>         echo ${x^^} → HELLO
>         
>         
>         So far, so good.
>         
>         
>         x="HELLO"
>         echo ${x} → HELLO
>         echo ${x,} → hELLO
>         echo ${x,,} → hello
>         
>         
>         Still good.
>         
>         
>         echo ${x^} → HELLO
>         echo ${x^,} → HELLO
>         
>         
>         Question: How to do ”HELLO” → ”Hello” in one step?
>         
>         
>         I can do it in two steps, of course:
>         y=${x,,}; echo ${y^} → Hello
>         
>         
>         
>         
>         Johnny Rosenberg
>         
> 
> 
> It doesn't look like you can use ${} inside another ${} with bash.
> 
> 
> I found these alternatives :
> 
> s="HELLO"
> echo $s | sed 's/.*/\L&/; s/[a-z]*/\u&/g'
> 
> Hello
> 
> 
> or use zsh.
> 
> 
> s="HELLO"
> 
> echo ${(C)${(L)s}}
> 
> Hello
> 
> 
> s="THIS IS A TEST"
> 
> echo ${(C)${(L)s}}
> This Is A Test

Interesting. I wonder which is more expensive (in terms of system
resources) calling sed to do it, or just doing the two step with bash...

-- 
W. Scott Lockwood III
AMST/AMC Technology
GWB2009033817





More information about the ubuntu-users mailing list