21 May 2018
It is often useful to change the case of a variable. A hostname may need converting, or maybe you
want to be sure to get somebody's name properly capitalised: Steve
, as opposed to
steve
, STEVE
or even sTeVe
.
The old way to do this was with the tr
command, either in the GNU form (which understands keywords :lower: and :upper:), like this:
echo $name | tr '[:lower:]' '[:upper:]'
Or in the non-GNU form, using a range, like this:
echo $name | tr '[a-z]' '[A-Z]'
Either way, this was a bit ugly, and a bit of a hassle.
Bash provides an alternative method. From the man
page, it isn't very clear what it
does, so many people are quite unaware of this useful facility.
The ^
operator converts to uppercase, while ,
converts to lowercase.
If you double-up the operators, ie, ^^
or ,,
, it applies to the whole string; otherwise,
it applies only to the first letter (that isn't absolutely correct - see "Advanced Usage" below - but for most uses, it's an adequate description).
For example, if you want to ensure that a name read in is all in lowercase, you can do this:
#!/bin/bash read -p "What is your first name? " firstname firstname=${firstname,,} echo "Hello, ${firstname}."
When run, this will take any input you give it, and convert it all to lowercase:
$ ./case.sh What is your first name? steve Hello, steve. $ ./case.sh What is your first name? STEVE Hello, steve. $ ./case.sh What is your first name? StEvE Hello, steve. $
You can then take this further, by using the single ^
operator. This will convert just the first letter to uppercase
(and of course a single ,
would convert it to lowercase).
#!/bin/bash read -p "What is your first name? " firstname firstname=${firstname,,} firstname=${firstname^} echo "Hello, ${firstname}."
When you run this, it first converts the whole thing to lowercase, as before, but then converts the first letter to uppercase, to fit with the standard convention of a name having an initial uppercase character, followed by lowercase:
$ ./case.sh What is your first name? steve Hello, Steve. $ ./case.sh What is your first name? STEVE Hello, Steve. $ ./case.sh What is your first name? StEvE Hello, Steve. $
There is another feature to this transformation, although it is hard to find a use for it. You can provide a single-character pattern to match.
If you use the single ,
or ^
, only the first character in the string (note - this is not the first matching character,
but the first character, whatever it may be) is compared, and converted if it matches. If you use ,,
or ^^
then all
matching characters are converted.
One example would be:
#!/bin/bash read -p "Enter some lowercase text: " text echo "I have highlighted the letter 'E' for you:" echo ${text^^e} $
You could run this, and highlight the letter 'E' any time it is mentioned, by capitalising it:
$ ./case2.sh Enter some lowercase text: abcdefghiabcdefghi I have highlighted the letter 'E' for you: abcdEfghiabcdEfghi $
This could be made more useful, by making the highlighted letter itself variable:
#!/bin/bash read -p "Enter some lowercase text: " text read -p "Which letter do you want to highlight?: " highlight echo "I have highlighted that letter for you:" echo ${text^^$highlight}
When run, this would capitalise whichever letter you asked it to:
$ ./case2.sh Enter some lowercase text: abcdefghiabcdefghi Which letter do you want to highlight?: d I have highlighted that letter for you: abcDefghiabcDefghi $ ./case2.sh Enter some lowercase text: abcdefghiabcdefghi Which letter do you want to highlight?: g I have highlighted that letter for you: abcdefGhiabcdefGhi $
My Shell Scripting books, available in Paperback and eBook formats. This tutorial is more of a general introduction to Shell Scripting, the longer Shell Scripting: Expert Recipes for Linux, Bash and more book covers every aspect of Bash in detail.
![]() Shell Scripting Tutorial is this tutorial, in 88-page Paperback and eBook formats. Convenient to read on the go, and in paperback format good to keep by your desk as an ever-present companion. Also available in PDF form from Gumroad:Get this tutorial as a PDF | ![]() Shell Scripting: Expert Recipes for Linux, Bash and more is my 564-page book on Shell Scripting. The first half covers all of the features of the shell in every detail; the second half has real-world shell scripts, organised by topic, along with detailed discussion of each script. |