Case Conversion
tr.In this page:
Uppercase and Lowercase the Whole String
${var^^} converts every character in the string to uppercase, and ${var,,} converts every character to lowercase, both done natively by Bash without spawning an external process. This is a fast, built-in alternative to piping through tr.
Example: Uppercase and Lowercase the Whole String
#!/bin/bash
name="Ada Lovelace"
echo "Uppercase: ${name^^}"
echo "Lowercase: ${name,,}"
Login to try C/C++/Java/PHP code in the editor
Capitalizing Just the First Character
${var^} uppercases only the first character of the string, leaving the rest untouched, and ${var,} does the same for lowercasing just the first character. This is useful for capitalizing a word without affecting the rest of it.
Example: Capitalizing Just the First Character
#!/bin/bash
word="hello"
capitalized=${word^}
echo "Capitalized: $capitalized"
shouty="WORLD"
lowered_first=${shouty,}
echo "First letter lowered: $lowered_first"
Login to try C/C++/Java/PHP code in the editor
Converting Only Matching Characters
Both ^^ and ,, accept an optional glob pattern restricting which characters get converted, so ${var^^[aeiou]} uppercases only the vowels in the string while leaving consonants as they are. This gives fine-grained control beyond an all-or-nothing conversion.
Example: Converting Only Matching Characters
#!/bin/bash
word="programming"
vowels_upper=${word^^[aeiou]}
echo "Vowels uppercased: $vowels_upper"
Login to try C/C++/Java/PHP code in the editor
Fallback for Older Bash: tr
On Bash versions older than 4.0 (like the stock /bin/bash shipped on some macOS systems), ${var^^}/${var,,} are not available, so tr '[:lower:]' '[:upper:]' (or vice versa) piped through is the portable alternative. It's slower since it spawns an external process, but works everywhere.
Example: Fallback for Older Bash: tr
#!/bin/bash
name="Ada Lovelace"
upper=$(echo "$name" | tr '[:lower:]' '[:upper:]')
echo "Uppercase via tr: $upper"
Login to try C/C++/Java/PHP code in the editor
- Assuming case conversion parameter expansion (
^^,,,) works in old Bash versions; it was introduced in Bash 4.0, so scripts targeting very old Bash (like macOS's stock 3.2) needtrinstead. - Confusing the single-character forms (
^,,) with the double-character forms (^^,,,); the single form only affects the first character, the double form affects the entire string. - Forgetting these operators only work on plain variables in
${var^^}-style expansions, not directly on string literals -- you must store the text in a variable first.
${var^^}converts the entire string to uppercase;${var,,}converts the entire string to lowercase.${var^}uppercases only the first character;${var,}lowercases only the first character.- These are Bash 4.0+ features; older Bash (e.g. macOS's default 3.2) requires
tr '[:lower:]' '[:upper:]'instead. - You can also convert only characters matching a specific pattern, e.g.
${var^^[aeiou]}uppercases only vowels.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: