You're viewing a comment by sergio and its responses.

June 01, 2012, 19:24

In order to get lowercase or uppercase from a variable. Let's see an example:

USER is a shell variable with the current user. For the purpose USER="admin".

- Uppercase
echo ${USER^} # Admin (min match)
echo ${USER^^}# ADMIN (max match)

Now we have te opposite situation, USER="DBADMIN"

- Lowercase
echo ${USER,} # dBADMIN (min match)
echo ${USER,,} # dbadmin (max match)^

Most of the people would be amazing with the funcionalities you can find on bash.

Nice post ;)

Comment Responses

June 01, 2012, 19:46

These are awesome! I'll cover these when I write about working with strings.

June 03, 2012, 10:21

I didn't knew that I will must use it!
Thanks! :-)

Gaurav Permalink
June 05, 2012, 17:06

nice one .....really this is an amazing thing it is also working on other variable also ----

$ echo $SHELL

$ echo ${SHELL^^}

June 08, 2012, 10:51

Be careful with using this feature of bash, as it requires bash version 4 or higher. So on a mac (including Lion), this functionality isn't there.

dave @ [ bahamas10 :: (Darwin) ] ~ $ echo "$BASH_VERSION"
dave @ [ bahamas10 :: (Darwin) ] ~ $ echo "${BASH_VERSION^^}"
-bash: ${BASH_VERSION^^}: bad substitution
dave @ [ bahamas10 :: (Darwin) ] ~ $ 

The only safe way to do this in older versions of bash is to use tr, and even then, to make sure you are using a locale-safe way of translating text.


dave @ [ bahamas10 :: (Darwin) ] ~ $ echo "$BASH_VERSION" | tr '[[:lower:]]' '[[:upper:]]'

Ugly, I know, but safe.

Reply To This Comment

(why do I need your e-mail?)

(Your twitter handle, if you have one.)

Type the word "computer_325": (just to make sure you're a human)

Please preview the comment before submitting to make sure it's OK.