Capitalize the First Letter in a String or Make It Lowercase in Bash

This can be handy for outputting labels in a human readable way.
This requires Bash 4+ or above so if you’re on macOS you’ll want to brew install bash. This takes advantage of parameter
expansion.
If you plan to follow along in zsh, you’ll want to run bash first.
Capitalize the first character:
greeting="hello world"
echo "${greeting^}"
Hello world
Lowercase the first character:
greeting="Hello world"
echo "${greeting,}"
hello world
In both cases it modifies the first character. For example heLLo with ^
will return HeLLo.
You can also use ^^ or ,, to uppercase or lowercase all characters in a
string, in the above examples that would be HELLO WORLD or hello world.
The video below demos the above.
# Demo Video
Timestamps
- 0:35 – Capitalize first character
- 0:59 – Lowercase first character
- 1:22 – Uppercase or lowercase all characters in a string
When was the last time you did this? Let me know below.