Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

First character of a variable in a shell script to uppercase?

Tags:

shell

unix

sed

awk

I have a shell script that starts unit tests for modules. I need the name of the module in all lowercase and with the first character uppercase. So far I have been doing it like this:

#!/bin/sh -x                                                                                                                                                                          
# z.B. getbrowser                                                                                                                                                                     
strModuleToTest=$1

# g                                                                                                                                                                                   
strModuleToTestUppercaseFirstletter=${strModuleToTest:0:1}
# etbrowser                                                                                                                                                                           
strModuleToTestUppercaseLastletters=${strModuleToTest:1}

# g -> G                                                                                                                                                                              
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/a/A}"
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/b/B}"
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/c/C}"
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/d/D}"
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/e/E}"
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/f/F}"
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/g/G}"
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/h/H}"
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/i/I}"
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/j/J}"
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/k/K}"
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/l/L}"
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/m/M}"
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/n/N}"
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/o/O}"
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/p/P}"
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/q/Q}"
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/r/R}"
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/s/S}"
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/t/T}"
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/u/U}"
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/v/V}"
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/w/W}"
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/x/X}"
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/y/Y}"
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/z/Z}"

# Getbrowser
strModuleToTestUppercase=$strModuleToTestUppercaseFirstletter""$strModuleToTestUppercaseLastletters

What would be an easier way to set $strModuleToTestUppercase? I tried

strModuleToTestUppercase="${strModuleToTest[@]^}"

or

strModuleToTestUppercasesed="sed 's/\<./\u&/g' $strModuleToTest"

but without success.

like image 946
mles Avatar asked Sep 14 '12 07:09

mles


People also ask

How do you capitalize the first letter in a shell script?

You can convert the case of the string more easily by using the new feature of Bash 4. '^' symbol is used to convert the first character of any string to uppercase and '^^' symbol is used to convert the whole string to the uppercase.

How do I make the first letter capital in Bash?

Use ^ to convert the first letter to uppercase and ^^ to convert all characters to uppercase. Use , to convert the first letter to lowercase and ,, to convert all characters to lowercase. Use ~ to toggles the case for the first character and ~~ to toggle cases for all characters.

What is $() in shell script?

Seems like ${variable} is the same as $variable , while $() is to execute a command. Why use ${} then? linux.

How do you get the first character of a string in shell?

To access the first character of a string, we can use the (substring) parameter expansion syntax ${str:position:length} in the Bash shell. position: The starting position of a string extraction. length: The number of characters we need to extract from a string.


1 Answers

If:

s=somemodule

with bash v4+

echo ${s^}

This should work with a bit older bash versions (from Glenn):

echo $(tr a-z A-Z <<< ${s:0:1})${s:1}")

with zsh

echo ${(C)s}

with ash and coreutils

echo $(echo $s | cut -c1 | tr a-z A-Z)$(echo $s | cut -c2-)

with GNU sed

echo $s | sed 's/./\U&/'

with BSD sed

echo $s | sed '
  h;
  y/quvwxzdermatoglyphicsbfjkn/QUVWXZDERMATOGLYPHICSBFJKN/;
  G;
  s/\(.\)[^\n]*\n.\(.*\)/\1\2/;
'

with awk

echo $s | awk '{ print toupper(substr($0, 1, 1)) substr($0, 2) }'

with perl

echo $s | perl -nE 'say ucfirst'

with python

echo $s | python -c 'import sys; print sys.stdin.readline().rstrip().capitalize()'

with ruby

echo $s | ruby -e 'puts ARGF.read.capitalize'

Output in all cases

Somemodule
like image 145
Thor Avatar answered Oct 02 '22 08:10

Thor