How to convert a string to uppercase in Bash
July 9, 2022
This note will show you how to convert a string to upper case (capital letters, uppercase) on the Linux command line.
To convert a string to capital letters regardless of its current case, use one of the following commands.
tr
echo "Hi all" | tr '[:lower:]' '[:upper:]' HI ALL
Attention! If you want to change the case of any letters other than Latin (national alphabets, letters with diacritics), then do not use tr, but use any other solution suggested below. This is because the classic Unix tr operates on single-byte characters and is not compatible with Unicode.
AWK
echo "Hi all" | awk '{print toupper($0)}' HI ALL
Bash
a="Hi all" echo "${a^^}" HI ALL
Starting with Bash 5.1, there is a conversion option U, which is intended to convert a string to uppercase:
${var@U}
Example:
v="heLLo" echo "${v@U}" HELLO
sed
echo "Hi all" | sed -e 's/\(.*\)/\U\1/' HI ALL
Or this solution:
echo "Hi all" | sed -e 's/\(.*\)/\U\1/' <<< "$a" HI ALL
Another solution:
echo "Hi all" | sed 's/./\U&/g'
Perl
echo "Hi all" | perl -ne 'print uc' HI ALL
Python
a="Hi all" b=`echo "print ('$a'.upper())" | python`; echo $b
Ruby
a="Hi all" b=`echo "print '$a'.upcase" | ruby`; echo $b
PHP
b=`php -r "print strtoupper('$a');"`; echo $b
NodeJS
b=`node -p "\"$a\".toUpperCase()"`; echo $b
In zsh
a="Hi all" echo $a:u
Related articles:
- How to convert a string to lowercase in Bash (100%)
- How to remove newline from command output and files on Linux command line (75.9%)
- How to remove the first line from a file on the command line. How to remove the first 2, 3 or any other number of lines from a file (67.6%)
- How to print from specific column to last in Linux command line (62.7%)
- How to change the login shell in Linux. chsh instruction (59.1%)
- How to get data from a web page using GET and POST methods in a Python script on Windows (RANDOM - 1.7%)