[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: case modification won't work with pattern

From: Greg Wooledge
Subject: Re: case modification won't work with pattern
Date: Thu, 10 Mar 2011 08:31:55 -0500
User-agent: Mutt/

On Thu, Mar 10, 2011 at 10:22:12AM +0800, Jerry Wang wrote:
>       var="abcabc"
>       echo "var: ${var}"
>       echo "replace the leading \"ab\" to uppercase: ${var^ab}" # expect to 
> get "ABcabc" ?

The documentation is a bit terse, admittedly.  What the ^ operator
does is compare the *first character* of var to the *glob pattern* which
follows the ^.  If the character matches the glob, it gets capitalized.

No single character is ever going to match the glob "ab", because it's
two characters long.

The closest you're going to get is:

imadev:~$ var=abcabc; echo "${var^[ab]}"

The leading "a" matches the glob "[ab]" and so it's capitalized.

A better description of ^ might be "conditional capitalization".  Its
purpose is to capitalize the first character of a string, or of each
string in an array, because people frequently want that.

imadev:~$ arr=(the quick brown fox); echo "address@hidden"
The Quick Brown Fox

Specifying a glob after the ^ lets you skip certain letters, if for some
reason you wanted to do that.  I can't think of any real-life examples
where that would be desirable, at the moment.

>       echo "replace all the \"ab\" to uppercase: ${var^^ab}"    # expect to 
> get "ABcABc" ?

What ^^ does is compare *each* character of var, one by one, to the glob
pattern that follows the ^^.  It doesn't operate on multi-character

You can use ^^ to capitalize every a and every b in var, but you cannot
use it to capitalize only instances of "ab" without also capitalizing
"a" and "b" in isolation:

imadev:~$ var=abcacb; echo "${var^^[ab]}"

More often, it's used to capitalize every character in a string:

imadev:~$ arr=(the quick brown fox); echo "address@hidden"

Prior to bash 4, the only way to do that was to call tr(1).

reply via email to

[Prev in Thread] Current Thread [Next in Thread]