0
0
Bash-scriptingConversionBeginner · 2 min read

Bash Script to Convert String to Lowercase

In Bash, you can convert a string to lowercase using lowercase_string=${string,,}, which changes all uppercase letters in string to lowercase.
📋

Examples

InputHELLO
Outputhello
InputBash Script 123
Outputbash script 123
Inputalready lowercase
Outputalready lowercase
🧠

How to Think About It

To convert a string to lowercase in Bash, think about changing each uppercase letter to its lowercase equivalent. Bash provides a simple way to do this by using the ,, operator inside parameter expansion, which automatically transforms all uppercase letters in the string to lowercase.
📐

Algorithm

1
Get the input string.
2
Use Bash parameter expansion with <code>,,</code> to convert all uppercase letters to lowercase.
3
Store the result in a new variable or overwrite the original.
4
Print or return the lowercase string.
💻

Code

bash
#!/bin/bash

input="Hello World!"
lowercase_string=${input,,}
echo "$lowercase_string"
Output
hello world!
🔍

Dry Run

Let's trace the input "Hello World!" through the code

1

Set input string

input="Hello World!"

2

Convert to lowercase

lowercase_string=${input,,} -> "hello world!"

3

Print result

echo prints "hello world!"

inputlowercase_string
Hello World!hello world!
💡

Why This Works

Step 1: Parameter Expansion

The ${variable,,} syntax is Bash's parameter expansion that converts all uppercase letters in variable to lowercase.

Step 2: No External Commands

This method uses built-in Bash features, so it is fast and does not require calling external programs like tr.

Step 3: Simple and Readable

The syntax is concise and easy to read, making scripts cleaner and easier to maintain.

🔄

Alternative Approaches

Using tr command
bash
#!/bin/bash
input="Hello World!"
lowercase_string=$(echo "$input" | tr '[:upper:]' '[:lower:]')
echo "$lowercase_string"
This uses an external command <code>tr</code>, which is portable but slower than Bash parameter expansion.
Using awk
bash
#!/bin/bash
input="Hello World!"
lowercase_string=$(echo "$input" | awk '{print tolower($0)}')
echo "$lowercase_string"
This uses <code>awk</code> to convert to lowercase, useful if you already use awk in your script but slower than built-in Bash.

Complexity: O(n) time, O(n) space

Time Complexity

The operation processes each character once to convert uppercase letters to lowercase, so it runs in linear time relative to string length.

Space Complexity

It requires space proportional to the input string length to store the converted string, so space complexity is linear.

Which Approach is Fastest?

Bash parameter expansion is fastest because it avoids spawning external processes, unlike tr or awk.

ApproachTimeSpaceBest For
Bash parameter expansion (${string,,})O(n)O(n)Fastest, simplest for Bash scripts
tr commandO(n)O(n)Portable, works in older shells
awk commandO(n)O(n)Useful if awk is already used
💡
Use Bash's built-in ${string,,} for fast and simple lowercase conversion without external commands.
⚠️
Beginners often forget to quote variables, which can cause word splitting or errors when the string contains spaces.