Translate

Image of Linux Kernel Development (3rd Edition)
Image of Advanced Programming in the UNIX Environment, Second Edition (Addison-Wesley Professional Computing Series)
Image of Beginning Google Maps API 3
Image of XSLT 2.0 and XPath 2.0 Programmer's Reference (Programmer to Programmer)

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

Comments are closed.