Bash:
Converting a string to lower case
How to:
Here’s the simple way to convert a string to lower case in Bash:
str="Make Me Lower Case"
lower_str=$(echo "$str" | tr '[:upper:]' '[:lower:]')
echo $lower_str
Output:
make me lower case
Bash 4.0 and above has a built-in way with parameter expansion:
str="Make Me Lower Case"
lower_str="${str,,}"
echo $lower_str
Output:
make me lower case
Deep Dive
Before Bash 4.0, commonly used methods to convert strings to lower case involved external utilities like tr
, awk
, or sed
. Each of these provide different ways to manipulate strings beyond simply changing case, but may need to spawn a new process, affecting performance.
The introduction of ${parameter,,pattern}
syntax in Bash 4.0 provided a native feature to transform strings which is faster and doesn’t rely on external utilities. There are alternatives within Bash itself:
awk
:echo $str | awk '{print tolower($0)}'
sed
:echo $str | sed 's/[A-Z]/\L&/g'
tr
:echo $str | tr '[:upper:]' '[:lower:]'
- as shown above.
In terms of implementation, ${parameter,,pattern}
don’t just alter ASCII characters; they’re UTF-8 aware and can handle non-English characters, making them versatile for international applications.
See Also
- Bash Parameter Expansion: https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html
tr
Command: https://www.gnu.org/software/coreutils/manual/html_node/tr-invocation.html- AWK Programming: https://www.gnu.org/software/gawk/manual/gawk.html
sed
Stream Editor: https://www.gnu.org/software/sed/manual/sed.html