There are many ways to convert a string to uppercase or lowercase in Linux. The most commonly used commands to change case are tr, sed and awk. Tr is the simplest command for this task.
From Bash 4, there are certain symbols which allows to convert the string case.
In this guide, we learn how to convert the string to uppercase and lowercase on Linux.
Using TR to convert case
The tr command is used for translating and deleting characters. It can be used to convert a string or file content to uppercase or lowercase. Its is very commonly used in bash scripts.
[:upper:] or [A-Z] - indicate Uppercase
[:lower:] or [a-z] - indicate Lowercase
To convert any string from uppercase to lowercase, type:
tr [:upper:] [:lower:]
To convert any string from lowercase to uppercase, type:
tr a-z A-Z
For example to convert every letter in the string ''welcome' to capital letter, type:
$ echo welcome | tr [:lower:] [:upper:]
For example, lets use tr command in a bash script to convert a string to lower case
!/bin/bash
y="WELCOME"
val=$(echo $y | tr '[:upper:]' '[:lower:]')
string="$val linux"
echo $string
To convert the content of the text file named file.txt from lower case to uppercase
$ tr a-z A-Z < ./file.txt
Vise versa to convert file content to lowercase, type
$ tr A-Z a-z < ./myfile.txt
Using SED to convert case
Sed command is used to Linux to parse and transform text. Using sed its possible to convert the case of the string.
Using sed, to convert string to upper case, type
echo "hello world" | sed 's/[a-z]/\U&/g'
[a-z]
is the regular expression which will match lowercase letters. \U&
is used to replace these lowercase letters with the uppercase version.
Now to change to lower case using the following command:
echo "HELLO WORLD " | sed 's/[A-Z]/\L&/g'
[A-Z]
is the regular expression which will match uppercase letters. \L&
is used to replace these uppercase letters with the lowercase version.
To convert text from file to uppercase using command line tool sed, we use the following pattern:
$ sed 's/[a-z]/\U&/g' < ./myfile.txt
Can also use the expression 's/.*/\L&/g' for the same.
Using AWK to convert case
The awk command uses tolower
and toupper
functions to convert a string case.
To convert a string to uppercase, type
string="hello world"
echo "$string" | awk '{print toupper($0)}'
To convert the all contents in a text file named file.txt to uppercase, use:
$ awk '{ print toupper($0) }' ./file.txt
Likewise to convert to lowercase, use:
$ awk '{print tolower($0)}' ./file.txt
Convert case using symbols in Bash
From Bash version 4, a new feature was introduced to convert the case of the string.
1 | Convert the first character of any string to uppercase | ^ |
2 | Convert the whole string to the uppercase | ^^ |
3 | Convert the first character of the string to lowercase | , |
4 | Convert the whole string to the lowercase. | ,, |
Example 1:
Convert first character of the string using ^
symbol:
$ note='welcome'
$ echo $note
$ echo ${note^}
Example 2:
Convert whole bash variable to Upper case using ^^
symbol:
$ note='welcome'
$ echo $note
$ echo ${note^^}
Example 3:
Convert to lowercase using ,,
symbol:
$ note='WELCOME'
$ echo ${note,,}
Conclusion
In this guide, we learned how to convert to uppercase or lowercase in Linux. If you have any questions, we are happy to clarify them for you.
Comments