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]}" Abcabc 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 "${arr[@]^}" 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 substrings. 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]}" ABcAcB More often, it's used to capitalize every character in a string: imadev:~$ arr=(the quick brown fox); echo "${arr[@]^^}" THE QUICK BROWN FOX Prior to bash 4, the only way to do that was to call tr(1).