← Back to Bash Course | Chapter 7: String Manipulation | Lesson 4 of 10

Case Conversion

Bash can convert a string to uppercase or lowercase directly through parameter expansion, without needing an external tool like tr.

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

bash
#!/bin/bash
name="Ada Lovelace"
echo "Uppercase: ${name^^}"
echo "Lowercase: ${name,,}"

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

bash
#!/bin/bash
word="hello"
capitalized=${word^}
echo "Capitalized: $capitalized"
shouty="WORLD"
lowered_first=${shouty,}
echo "First letter lowered: $lowered_first"

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

bash
#!/bin/bash
word="programming"
vowels_upper=${word^^[aeiou]}
echo "Vowels uppercased: $vowels_upper"

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

bash
#!/bin/bash
name="Ada Lovelace"
upper=$(echo "$name" | tr '[:lower:]' '[:upper:]')
echo "Uppercase via tr: $upper"
Common Mistakes
  1. 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) need tr instead.
  2. 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.
  3. 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.
Chapter Summary
  • ${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.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.