0
0
Bash Scriptingscripting~5 mins

Uppercase and lowercase conversion in Bash Scripting

Choose your learning style9 modes available
Introduction
Changing text to uppercase or lowercase helps make data consistent and easier to read or compare.
You want to make user input case-insensitive.
You need to format filenames or text to a standard case.
You want to compare two strings without case differences.
You are preparing text data for reports or logs.
Syntax
Bash Scripting
variable=${variable^^}  # Convert to uppercase
variable=${variable,,}  # Convert to lowercase
Use ${variable^^} to convert all letters in the variable to uppercase.
Use ${variable,,} to convert all letters in the variable to lowercase.
Examples
Converts the string 'hello' to uppercase and prints 'HELLO'.
Bash Scripting
name="hello"
echo ${name^^}
Converts the string 'WORLD' to lowercase and prints 'world'.
Bash Scripting
name="WORLD"
echo ${name,,}
Shows both uppercase and lowercase conversions of 'Bash Scripting'.
Bash Scripting
text="Bash Scripting"
echo ${text^^}
echo ${text,,}
Sample Program
This script takes a string, converts it to uppercase and lowercase, then prints all versions.
Bash Scripting
#!/bin/bash

input="Hello Bash"

# Convert to uppercase
upper=${input^^}

# Convert to lowercase
lower=${input,,}

# Print results
echo "Original: $input"
echo "Uppercase: $upper"
echo "Lowercase: $lower"
OutputSuccess
Important Notes
This uppercase/lowercase conversion works only on letters; numbers and symbols stay the same.
Make sure your script uses bash version 4 or higher for this syntax to work.
You can convert only the first letter by using ${variable^} or ${variable,}.
Summary
Use ${variable^^} to convert all letters to uppercase.
Use ${variable,,} to convert all letters to lowercase.
This helps keep text consistent and easy to compare.