Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a builtin way in KornShell to capitalize a word, e.g. korn -> Korn? A Bash 4 example to clarify:

echo "${str^}"

If there is not a bultin way to do this in KornShell, what is the most concise and efficient way to write a function that will do it?

share|improve this question
bash can do this with M-c (typically, Alt-c) at the beginning of the word. No idea about ksh though. – jw013 Jul 16 '12 at 15:04
@Kazark, better specify whether you want editing or scripting functionality. My understanding was that you want something like str='korn'; echo "${str^}" in bash. But after @jw013's comment I am not so sure. – manatwork Jul 16 '12 at 15:08
@manatwork Thanks. Done. – Kazark Jul 16 '12 at 16:06
up vote 3 down vote accepted

Here is a pure ksh (ksh93) way:

function cap {
    typeset -u f
    printf "%s%s\n" "$f" "${1:1}"

$ cap korn
share|improve this answer
Not excessively verbose. +1 ...I didn't know the syntax for string slicing, so that is helpful. – Kazark Jul 16 '12 at 20:56

If you don't have ksh93 available, you can do it in standard ksh like this:

function capitalize { 
  typeset -u first
  print "${first}${1#?}"


${1#?} strips all characters matching "?" from the beginning of the first arg. i.e. strips off the first character

${var%pattern} strips off pattern from the end of var. If we use the original word minus the first letter as the pattern (${1#?}), we get the first letter back.

typeset -u turns the first letter uppercase

$ capitalize "the funky chicken"
The funky chicken

Caveat, this actually doesn't always work since you could have special regex characters after the first letter of the word and they will be interpreted as such by ${var%pattern}. e.g.

$ capitalize a*
share|improve this answer
you might want print -- .... I can't recall how ksh's print handles -options. – mikeserv Feb 9 '15 at 18:22

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.