How to Convert a String to Lower Case in Bash?

Converting a string to lowercase can be helpful in various situations, such as comparing strings, searching strings, or formatting output. This is quite effective in creating a credential-based service on the system and, thus is a widely used practice in encryption. 

This article will discuss various methods to convert a string to lowercase in Linux and provide examples for each method mentioned below.

  • Using the tr Command
  • Using the awk Command
  • Using the sed Command
  • Using the Bash String Manipulation
  • Using the Bash Built-in Function

Method 1: Using the tr Command

The tr command is a Linux command that translates or deletes characters from a string. A user can use the tr command to convert a string to lowercase by specifying the range of characters to be converted to lowercase. The syntax of the tr command is as follows:

echo "STRING" | tr '[:upper:]' '[:lower:]'

Here, the echo command is used to print the string, and the tr command converts the string to lowercase. The [:upper:] and [:lower:] are character classes that represent all uppercase and lowercase characters, respectively.

Example:

#!/bin/bash
echo "HELLO WORLD" | tr '[:upper:]' '[:lower:]'

The output can be seen by executing the bash script below:

$ bash lower_case.sh

Method 2: Using the awk Command

The awk command is a Linux command used to manipulate and process text files. Users can use the awk command and the tolower() function to convert a string to lowercase. The tolower() function converts all uppercase characters to lowercase characters.

The syntax of the awk command is as follows:

echo "STRING" | awk '{print tolower($0)}'

Here, the echo command is used to print the string, and the awk command is used to convert the string to lowercase using the tolower() function.

Example:

#!/bin/bash
echo "HELLO WORLD" | awk '{print tolower($0)}'

The same output will be generated by executing this script which is mentioned below:

$ bash lower_case.sh

Method 3: Using the sed Command

The sed command is a Linux command that performs text transformations on an input file or stream. A user can use the sed command and s command to convert a string to lowercase. The s command replaces the matched pattern with the specified string.

The syntax of the sed command is as follows:

echo "STRING" | sed 's/.*/\L&/'

Here, the sed command is used to convert the string to lowercase using the s command, and the echo command will print the output. The \L is an escape character used to indicate the start of the lowercase conversion. The & is a special character used to represent the entire matched pattern.

Example:

#!/bin/bash
echo "HELLO WORLD" | sed 's/.*/\L&/'

This code will generate the same output after execution as shown below:

$ bash lower_case.sh

Method 4: Using the Bash String Manipulation

Bash string manipulation is a feature of Bash that allows users to manipulate strings in various ways. They can use the Bash string manipulation to convert a string to lowercase using the ${string,,} syntax. The ${string,,} syntax converts all uppercase characters in the string to lowercase characters.

The syntax of the Bash string manipulation is as follows:

echo ${string,,}

Here, the string variable is set to the string to be converted to lowercase, and the ${string,,} syntax is used to convert the string to lowercase.

Example:

#!/bin/bash
string="HELLO WORLD"
echo ${string,,}

Executing this bash script will generate the same output as shown below:

$ bash lower_case.sh

Method 5: Using the Bash Built-in Function

The Bash built-in function is a feature of Bash that provides various useful functions. A user can use the Bash built-in function to convert a string to lowercase using the declare -l variable declaration. The declare -l variable declaration makes the variable automatically converted to lowercase.

The syntax of the Bash built-in function is as follows:

declare -l string="STRING"

syntax is used to declare the variable as a lowercase variable, and the echo $string command is used to print the lowercase string.

Example:

declare -l string="HELLO WORLD"
echo $string

This will provide the same output as well while executing this bash script as shown below:

$ bash lower_case.sh

Conclusion

Converting a string to lowercase is important in certain situations, especially if that string is case-sensitive. In this article, 5 different methods have been discussed that can be utilized to convert any string to lowercase. These five methods can be implemented using the tr command, the awk command, the sed command, the bash string manipulation, and the bash built-in function.