It covers how to convert a string to an Uppercase string in bash and shell script.

Upper case string calls if a string contains all letters in upper case. The output will be “HELLO WORLD WELCOME” if the input string contains the phrase “Hello World Welcome.”

Depending on the bash type and version, there are many ways to convert a string to upper case.

  • using the tr command

tr is called a translator, a command in Unix use to translate from one format to another.

here is the syntax.

tr input_format output_format

Here is a shell script for converting to upper case

message="This is a lowercase string converted to uppercase"
echo "$message" | tr '[:lower:]' '[:upper:]'

Output:

THIS IS A LOWERCASE STRING CONVERTED TO UPPERCASE

Another way to replace the above code

message="This is a lowercase string converted to uppercase"
echo "$message" | tr 'a-z' 'A-Z'

tr command works with ASCII and does not work with UTF characters.

  • using the awk command the string converts to upper case using the awk command toupper function combined with the awk command to result in an upper case string. The result is input to the echo command using the pipe operator.
message="It is a lowercase string converted to uppercase."
echo "$message" | awk '{print toupper($0)}'

It is best that work with ASCII and UTF characters.

  • in bash 4.0 version bash 4.0 provides string inbuilt in manipulation utilities. Adding two circumflexes (^) to a string makes a string in the upper case string.
message="This is a lowercase string converted to uppercase"
echo "${message^^}"
echo "${message}"
  • using Perl in the bash script

print lc with Perl makes a string in upper case letters

echo "$message" | perl -ne 'print uc'