tr💡 tr translates characters from one set to another.
echo "hello world" | tr '[:lower:]' '[:upper:]'
# HELLO WORLD
# Using POSIX character classes
echo "MiXeD CaSe" | tr '[:lower:]' '[:upper:]'
# MIXED CASE
# Using character ranges
echo "hello123" | tr 'a-z' 'A-Z'
# HELLO123
Convert filenames to uppercase:
for file in *.txt; do
mv "$file" "$(echo "$file" | tr '[:lower:]' '[:upper:]')"
done
💡 Unlike awk or sed, tr is specifically designed for character-by-character transformations, making it faster for simple case conversions.
Add these to your .bashrc or .zshrc for quick case conversion:
alias upcase="tr '[:lower:]' '[:upper:]'"
alias downcase="tr '[:upper:]' '[:lower:]'"
Now you can use them like:
echo "make me loud" | upcase
# MAKE ME LOUD
echo "MAKE ME QUIET" | downcase
# make me quiet
# Chain them together
echo "MiXeD" | downcase | upcase
# MIXED
# Or with files
cat README.md | upcase
🎉 Happy coding!