linux shell title case linux shell title case shell shell

linux shell title case


a GNU sed one-liner

echo something-that-is-hyphenated | sed -e 's/-\([a-z]\)/\u\1/g' -e 's/^[a-z]/\u&/'

\u in the replacement string is documented in the sed manual.


Pure bashism:

var0=something-that-is-hyphenatedvar1=(${var0//-/ })var2=${var1[*]^}var3=${var2// /}echo $var3SomethingThatIsHyphenated

Line 1 is trivial.
Line 2 is the bashism for replaceAll or 's/-/ /g', wrapped in parens, to build an array.
Line 3 uses ${foo^}, which means uppercase (while ${foo,} would mean 'lowercase' [note, how ^ points up while , points down]) but to operate on every first letter of a word, we address the whole array with ${foo[*]} (or ${foo[@]}, if you would prefer that).
Line 4 is again a replace-all: blank with nothing.
Line 5 is trivial again.


You can define a function:

hypenToCamel() {     tr '-' '\n' | awk '{printf "%s%s", toupper(substr($0,0,1)), substr($0,2)}'}CAMEL=$(echo something-that-is-hyphenated | hypenToCamel)echo $CAMEL