This tutorial explains how to convert a string to lowercase in a Bash script.

For example, if the input string is “Hello World Welcome,” the output will be “hello world welcome.”.

There are multiple ways to achieve this, depending on the Bash type and version.

  • using the tr command

The tr command, short for translator, is a Unix command used to convert characters from one format to another.

The syntax is as follows:

here is the syntax.

tr input_format output_format

Here’s a bash shell script using tr to convert a string to lowercase:

message="Hello World, Welcome."
echo "$message" | tr '[:upper:]' '[:lower:]'

Output:

hello world, welcome.

Alternatively, you can use.

message="Hello World, Welcome."
echo "$message" | tr 'A-Z' 'a-z'

Note: tr works with ASCII and does not support UTF characters.

  • using the awk command

To convert a string to lowercase using the awk command, the tolower function is combined with awk.

The result is then passed to the echo command using the pipe operator:

message="Hello World, Welcome"
echo "$message" | awk '{print tolower($0)}'

This method is best for both ASCII and UTF characters.

  • Use Perl in Bash Script Printing lc with Perl converts a string to lowercase.

lc is alias for lowercase.

echo "$message" | perl -ne 'print lc'
  • use Parameter expansion Bash 4.0 introduced inbuilt string manipulation utilities. To convert a string to lowercase, simply add two commas to the string. This is also called parameter expansion syntax.

The syntax is ${variable[options]}.

For example,

message="Hello World, Welcome"
echo "${message,,}"
echo "${message}"
msg="Hello World."
result="${msg,,}"
echo $result # hello world.

Here, ${msg,,} uses the ,, option to convert the variable to lowercase.

Note that this works in Bash version 4.0 and above.