Page
bash | string | uppercase | lowercase
Using Bash Parameter Expansion
Lowercase Conversion
To convert a string to lowercase, use the {variable,,} syntax:
string="Hello, World!"
lowercase_string="${string,,}"
echo "$lowercase_string"
Output:
Output:
hello, world!
Uppercase Conversion
To convert a string to uppercase, use the {variable^^} syntax:
string="Hello, World!"
uppercase_string="${string^^}"
echo "$uppercase_string"
Output:
HELLO, WORLD!
Using the `tr` Command
The tr (translate) command is a versatile tool for transforming text. It can be used for case conversion by specifying the input and output character sets:
Lowercase Conversion
string="Hello, World!"
lowercase_string=$(echo "$string" | tr '[:upper:]' '[:lower:]')
echo "$lowercase_string"
Output:
hello, world!
Uppercase Conversion
string="Hello, World!"
uppercase_string=$(echo "$string" | tr '[:lower:]' '[:upper:]')
echo "$uppercase_string"
Output:
HELLO, WORLD!
Output:
HELLO, WORLD!
Last updated