How to Convert Bash String to Lowercase

String manipulation is a fundamental aspect of scripting in any programming language, and Bash is no exception. One such common task is to convert the case of strings, which is particularly useful in text processing and analysis.

In this blog post, we’ll explore two methods of converting Bash string to lowercase - using the declare command and leveraging the power of parameter expansions. Let’s get started!

Prerequisite

To try out the scripts in this blog post, you need access to a Bash shell. You also need a text editor, such as "nano" or "vim", which come pre-installed by default in many Unix-like operating systems.

For the purpose of this blog post, I'll be using KodeKloud’s Ubuntu playground, which lets you access a pre-installed Ubuntu operating system in just one click. Best of all, you won't need to go through the hassle of installing any additional software— everything you need is already set up and ready to use.

Create a Script File

Let’s start by creating a Bash script file named demo.sh. This is where we'll place and run the scripts we're about to write in the upcoming sections. To create it, run the following command:

touch /usr/local/bin/demo.sh

Note: While you're free to create the demo.sh file in any directory of your choice, we're placing it in the /usr/local/bin directory for a specific reason. In most Linux distributions, this directory is included in the system's command path. This means we can run our script without making it executable.

With the script file created, let’s move on to the next section.

Convert Bash String to Lowercase Using Parameter Expansion

In Bash, you can convert strings to lowercase using a feature known as parameter expansion. A parameter expansion is a mechanism by which Bash retrieves and manipulates the value stored in a parameter, which is typically a variable. This mechanism allows us to convert the first character of a string to lowercase, convert all characters to lowercase, or even convert characters that match a specific pattern to lowercase.

Convert the first character to lowercase using ,

In Bash, you can use the ${parameter,} form of parameter expansion to modify the case of strings. When you place a single comma (,) after the parameter name, it directs Bash to convert the first character of the parameter value to lowercase.

Let’s see an example. Run the following command to open the demo.sh file using the "nano" text editor:

nano /usr/local/bin/demo.sh

Running this command brings up the "nano" text editor in your terminal as follows:

Next, add the following script to the editor:

#!/bin/bash

# Declare a string
greeting="Hello World"

# Use , to convert the first character to lowercase
echo "${greeting,}"

Notice that we have placed a single comma (,) after the parameter name greeting.

Once you've added the script, you'll need to save your work and exit the nano editor. To save, press ctrl + o. You'll see a message asking for the File Name to Write. Just press Enter to agree with the name that's already there. After your file is saved, you can leave "nano" by pressing ctrl + x.

Now, run the script using the following command:

bash demo.sh

You’ll see the following output:

As you can see, only the first character H of the string Hello World has been converted to lowercase h.

Convert all characters to lowercase using ,,

In the previous section, we learned how to convert only the first character of a string to lowercase using ${parameter,}. But what if you want to convert all the characters in the string to lowercase?

You can achieve this using the ${parameter,,} form of parameter expansion. Here, instead of a single comma, we use double commas (,,) after the parameter name.

Let's see an example. Run the following command to open the demo.sh file using the "nano" text editor:

#!/bin/bash

# Declare a string
greeting="Hello World"

# Use ,, to convert all characters to lowercase
echo "${greeting,,}"

Notice that we have placed double commas (,,) after the parameter name greeting.

Once you've added the script, you'll need to save your work and exit the editor. In "nano", you can do this by pressing ctrl+o to write the changes to the file, then press enter  to confirm the filename, and then press ctrl+x to close the editor.

Now, run the script using the following command:

bash demo.sh

You’ll see the following output:

As you can see, every character in the string Hello World has been converted to lowercase.

Convert to lowercase based on a pattern

It's also possible to convert specific parts of a string to lowercase based on a pattern. You can do this by using the ${parameter,pattern} or ${parameter,,pattern} form of parameter expansion. The pattern here represents the character or group of characters you wish to match.

With the double comma (,,) all matches of the pattern in the string are converted to lowercase. However, with the single comma (,) only the first character of the string is converted to lowercase, provided it matches the pattern.

It's important to note that if the pattern consists of more than one character, you need to place it within square brackets.

Here's an example to illustrate this. Run the following command to open the demo.sh file using the "nano" text editor:

nano /usr/local/bin/demo.sh

Next, add the following script to the editor:

#!/bin/bash

# Declare a string
greeting="HELLO WORLD"

# Converts the first letter to lowercase if it’s ‘H’ 
echo "${greeting,H}"

# Convert all occurrences of 'L' to lowercase
echo "${greeting,,L}"

# Convert all occurrences of any of the characters in 'WORLD' to lowercase
echo "${greeting,,[WORLD]}"

Notice how we've placed the pattern WORLD inside square brackets in the third command. Since WORLD consists of more than one character, wrapping it in square brackets is necessary for the pattern to be recognized correctly.

Once you've added the script, you'll need to save your work and exit the editor. In "nano", you can do this by pressing ctrl+o to write the changes to the file, then press enter to confirm the filename, and then press ctrl+x to close the editor.

Now, run the script using the following command:

bash demo.sh

When you run this script, it will produce three outputs like this:

In the first output, we see the initial H in our string HELLO WORLD has been converted to lowercase, thanks to the single comma operator. In the second output, the double comma operator converts all occurrences of L to lowercase. In the third output, it converts the substring WORLD to lowercase as well as the letters L and O in the word HELLO. That’s because these two letters are also in the word WORLD.

Parameter expansion using ~

As discussed earlier, using a single comma (,) or double commas (,,) before the closing brace in a parameter expansion (${parameter,} or ${parameter,,}) can convert the first character or all characters of the string to lowercase respectively.

However, there's an additional, less-known operator that can reverse the case of characters in a string. This involves using a tilde (~) instead of a comma in the parameter expansion.

Here's how it works:

  • ${parameter~}: This syntax reverses the case of the first character of the parameter value. If the first character is lowercase, it becomes uppercase, and vice versa.
  • ${parameter~~}: This syntax reverses the case of all characters in the parameter value. All lowercase characters become uppercase, and all uppercase characters become lowercase.

These operations also support pattern matching. If a pattern is specified, only the characters that match the pattern have their case reversed.

In essence, the tilde (~) functions as a case reversal operator. Since our post is primarily about converting strings to lowercase, this operator comes in handy only when we know the string we want to convert is all uppercase.

Let's put this into practice with an example. Run the following command to open the demo.sh file using the "nano" text editor:

nano /usr/local/bin/demo.sh

Next, add the following script to the editor:

#!/bin/bash

# Declare a variable
greeting="HELLO WORLD"

# Reverse the case of the first character
echo "${greeting~}"

# Reverse the case of all characters
echo "${greeting~~}"

Once you've added the script, you'll need to save your work and exit the editor. In "nano", you can do this by pressing ctrl+o to write the changes to the file, then press enter to confirm the filename, and then press ctrl+x to close the editor.

Now, run the script using the following command:

bash demo.sh

When you run this script, it will produce an output like this:

The first line is the result of the "${greeting~}" command. Here, only the first character of the greeting string has its case reversed. So, H in HELLO WORLD is converted to h, resulting in hELLO WORLD.

The second line comes from the "${greeting~~}" command. This time, all the characters in the greeting string have their cases reversed. As a result, all uppercase letters in HELLO WORLD are changed to lowercase, producing hello world.

Convert Bash String to Lowercase Using the Declare Command

Another useful command that Bash provides for string manipulation is the declare command. You can use this command to define variable types, and it comes with an option -l which converts the value of the variable into lowercase upon declaration.

Here's the syntax:

declare -l variable-name="Your string here"

In this syntax, the -l option tells Bash to convert the value of the string into lowercase.

Here's an example to illustrate this. Run the following command to open the demo.sh file using the "nano" text editor:

nano /usr/local/bin/demo.sh

Next, add the following script to the editor:

#!/bin/bash 

# Declare a string and convert it to lowercase
declare -l greeting="HELLO WORLD"

# Print the string
echo $greeting

Now, run the script using the following command:

bash demo.sh

When you run this script, it will output the string HELLO WORLD in all lowercase, as shown below:

Conclusion

In this blog post, we learned two ways to make Bash strings lowercase: using the declare command and using parameter expansion with , and ,,.

Use the declare command when you want to make a string lowercase right when you define it. If you need to change the string to lowercase after it’s been defined, use parameter expansion.

Looking to build a solid foundation in shell scripting or taking your existing skills to the next level? Check out these courses from KodeKloud:

  • Shell Scripts for Beginners: In this course, you'll dive into the practical world of Linux shell scripting. Regardless of your programming experience, you'll master fundamental scripting concepts such as variables, loops, and control logic. Throughout the course, you'll get plenty of hands-on experience using our comprehensive labs. Not only that, you'll also receive immediate feedback on your scripts, which will help you improve and refine them.
  • Advanced Bash Scripting: In this course, you'll start with fundamentals like variables, functions, and parameter expansions and then dive deeper into streams, input/output redirection, and command-line utilities like awk and sed. You'll master arrays for data manipulation and storage and learn best practices to create robust scripts.