Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linux shell title case

I am wrinting a shell script and have a variable like this: something-that-is-hyphenated.

I need to use it in various points in the script as:

something-that-is-hyphenated, somethingthatishyphenated, SomethingThatIsHyphenated

I have managed to change it to somethingthatishyphenated by stripping out - using sed "s/-//g".

I am sure there is a simpler way, and also, need to know how to get the camel cased version.

Edit: Working function derived from @Michał's answer

function hyphenToCamel {
    tr '-' '\n' | awk '{printf "%s%s", toupper(substr($0,1,1)), substr($0,2)}'
}

CAMEL=$(echo something-that-is-hyphenated | hyphenToCamel)
echo $CAMEL

Edit: Finally, a sed one liner thanks to @glenn

echo a-hyphenated-string | sed -E "s/(^|-)([a-z])/\u\2/g"
like image 939
Billy Moon Avatar asked Aug 06 '11 21:08

Billy Moon


People also ask

What does $() mean bash?

Example of command substitution using $() in Linux: Again, $() is a command substitution which means that it “reassigns the output of a command or even multiple commands; it literally plugs the command output into another context” (Source).

What does ${} mean in Linux?

$() – the command substitution. ${} – the parameter substitution/variable expansion.

What does $# mean in shell?

$# : This variable contains the number of arguments supplied to the script. $? : The exit status of the last command executed. Most commands return 0 if they were successful and 1 if they were unsuccessful. Comments in shell scripting start with # symbol.

What does #$ mean in bash?

#$ does "nothing", as # is starting comment and everything behind it on the same line is ignored (with the notable exception of the "shebang"). $# prints the number of arguments passed to a shell script (like $* prints all arguments). Follow this answer to receive notifications.


3 Answers

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.

like image 71
glenn jackman Avatar answered Oct 26 '22 15:10

glenn jackman


Pure bashism:

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

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.

like image 28
user unknown Avatar answered Oct 26 '22 14:10

user unknown


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
like image 36
Michał Šrajer Avatar answered Oct 26 '22 15:10

Michał Šrajer