This tutorial guides you through the process of converting a string to uppercase in bash and shell script.

An uppercase string refers to a string containing all letters in uppercase.

For instance, if the input string is “Hello World Welcome,” the output will be “HELLO WORLD WELCOME.”

Depending on the bash type and version, there are multiple methods to convert a string to uppercase.

  • using the tr command

The tr command, known as a translator, is a Unix command used to convert characters from one format to another.

The syntax is as follows:

tr input_format output_format

Here’s a shell script for converting to uppercase.

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'

Note: tr works with ASCII and does not support UTF characters.

  • using the awk command

To convert a string to uppercase using the awk command, the toupper function is combined with awk. The result is then passed 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 uc command in Perl converts a string into upper-case letters

echo "$message" | perl -ne 'print uc'
  • Use parameter expansion syntax Bash 4.0 provides built-in string manipulation utilities. Adding two circumflexes (^) to a string makes it an uppercase string, also called parameter expansion syntax.

Syntax is ${variable[options]}

variable to modify based on options. Options are optional that contains parameter expansion

#!/bin/bash

# Input string
message="hello, world!"

# Convert to uppercase
result="${message^^}"

# Display the result
echo "Original: $message"
echo "Uppercase: $result"

Parameter expansion syntax converts a string to uppercase. ${message^^} contains ^^ options to convert the variable message string into uppercase.

This feature is available in Bash version 4.0 and above.