Convert String to Uppercase

To convert a string to uppercase in Bash, use tr command. tr stands for translate or transliterate. With tr command we can translate lowercase characters, if any, in the input string to uppercase characters.

Syntax

The syntax to convert an input string to uppercase is

tr '[:lower:]' '[:upper:]'

tr command takes two two sets of characters as arguments.

With the above expression, if there are any lowercase characters in the given string, then tr converts it to uppercase.

ADVERTISEMENT

Example

In the following script, we take a string in s, and convert it to uppercase using tr command.

Example.sh

s="Hello World"
upperstr=$(echo $s | tr '[:lower:]' '[:upper:]')
echo "Original  String : $s"
echo "Uppercase String : $upperstr"

Output

Original  String : Hello World
Uppercase String : HELLO WORLD

Conclusion

In this Bash Tutorial, we have learnt how to convert a string to uppercase in Bash Script using tr command.