RSS Feed Share on Twitter

Buy this tutorial as a PDF for only $5!

All Shell Scripting Tips

21 May 2018

Changing to Uppercase or Lowercase

Changing the case of text in variables

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.
$ 

Advanced Usage

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
$ 

 

 


Books and eBooks

Contact

You can mail me with this form. If you expect a reply, please ensure that the address you specify is valid. Don't forget to include the simple addition question at the end of the form, to prove that you are a real person!

You can buy the content of this Shell Scripting Tutorial as a PDF!