First Letter Capitalization in Bash and Korn Shell

Bash does not have a built-in facility to first letter capitalize a string but Bash 4.0 and later has the necessary tools to do so as shown by the following example:

#!/bin/bash

Firstcap()
{
   lower=${1,,}
   echo ${lower^}
}

Firstcap lINUX

>>> OUTPUT IS: Linux

This function not only uppercases the first letter of the argument passed to it but also lowercases the remaining letters of the argument.

Here is how to do the same thing in ksh93:

#!/bin/ksh

Firstcap()
{
      typeset -u f
      f=${1:0:1}

      typeset -l r
      r=${1:1}

      echo "${f}${r}"
}

Firstcap lINUX

>>> OUTPUT IS: Linux           

Leave a Reply

  

  

  

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>