Convert a string to lowercase in Bash
Here are the top 3 ways for converting a string to lowercase in Bash.
Using tr
One of the most popular ways for lowercasing a string is using tr
. This is a POSIX standard method for accomplishing the task.
1echo "I WANT this LoWeRcAsEd" | tr '[:upper:]' '[:lower:]'
2# Outputs: i want this lowercased
Using awk
Another POXIS standard is using awk
’s native toLower
method.
1echo "I WANT this LoWeRcAsEd" | awk '{print tolower($0)}'
2# Outputs: i want this lowercased
Using Bash
4.0
bash
4.0 natively supports converting strings to lowercase.
1toLower="I WANT this LoWeRcAsEd"
2echo "${toLower,,}"
3# Outputs: i want this lowercased