Bash Script to Convert String to Uppercase
Use
echo "$string" | tr '[:lower:]' '[:upper:]' or Bash's built-in ${string^^} to convert a string to uppercase in a script.Examples
Inputhello world
OutputHELLO WORLD
InputBash123
OutputBASH123
Inputalready UPPER
OutputALREADY UPPER
How to Think About It
To convert a string to uppercase in Bash, you can either use the
tr command to translate all lowercase letters to uppercase or use Bash's built-in parameter expansion ${string^^} which changes all letters in the variable to uppercase. Both methods take the input string and produce the uppercase version.Algorithm
1
Get the input string from the user or variable.2
Use a method to convert all lowercase letters to uppercase.3
Output the converted uppercase string.Code
bash
#!/bin/bash input="Hello Bash" # Method 1: Using parameter expansion uppercase1=${input^^} echo "$uppercase1" # Method 2: Using tr command uppercase2=$(echo "$input" | tr '[:lower:]' '[:upper:]') echo "$uppercase2"
Output
HELLO BASH
HELLO BASH
Dry Run
Let's trace the input "Hello Bash" through the code using parameter expansion and tr command.
1
Set input string
input = "Hello Bash"
2
Convert using parameter expansion
uppercase1 = ${input^^} = "HELLO BASH"
3
Print uppercase1
Output: HELLO BASH
4
Convert using tr command
uppercase2 = $(echo "Hello Bash" | tr '[:lower:]' '[:upper:]') = "HELLO BASH"
5
Print uppercase2
Output: HELLO BASH
| Step | Operation | Value |
|---|---|---|
| 1 | input | Hello Bash |
| 2 | parameter expansion | HELLO BASH |
| 4 | tr command | HELLO BASH |
Why This Works
Step 1: Parameter Expansion
The ${string^^} syntax tells Bash to convert all lowercase letters in string to uppercase directly.
Step 2: tr Command
The tr command translates characters from lowercase to uppercase by mapping the character sets [:lower:] to [:upper:].
Alternative Approaches
awk command
bash
input="Hello Bash" uppercase=$(echo "$input" | awk '{print toupper($0)}') echo "$uppercase"
Uses awk's toupper function; slightly slower but useful if awk is preferred.
sed command
bash
input="Hello Bash" uppercase=$(echo "$input" | sed 's/.*/\U&/') echo "$uppercase"
Uses sed to convert to uppercase; less common but works in GNU sed.
Complexity: O(n) time, O(n) space
Time Complexity
The conversion processes each character once, so time grows linearly with string length.
Space Complexity
A new uppercase string is created, so space usage is proportional to the input string size.
Which Approach is Fastest?
Bash parameter expansion ${string^^} is fastest as it avoids spawning external commands.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Bash parameter expansion | O(n) | O(n) | Fastest, simplest, no external commands |
| tr command | O(n) | O(n) | Works in all shells, external command |
| awk command | O(n) | O(n) | When awk is preferred or needed |
| sed command | O(n) | O(n) | GNU sed environments, less common |
Use Bash's built-in
${string^^} for the simplest and fastest uppercase conversion.Forgetting to quote variables can cause word splitting or globbing issues when converting strings.