Console utility for capitalising/initialising words in string - for bash script?

Posted on 2004-11-28
Last Modified: 2013-12-15


I'm trying to do a translation to a text string in a bash script, to initialise each word in a string (so we're talking console standard input/output here).

I found the 'tr' utility and tried a few things including:

echo "Simon briggs" | tr ' a-z' ' A-Z'      *note the spaces*

produces "SIMON BRIGGS", when I really want "Simon Briggs"...

so it only appears to work on characters individually, when I want to check for a 'pair' of characters. This should hopefully explain what I mean at least.

Anyone have advice on a more suitable util - and perhaps some hints on the syntax required for it?


Question by:simonbriggs
    LVL 2

    Accepted Solution

    You probably want to use perl (specifically - "perl -e") ?  It's considerably more powerful, and understands all alphabets in all languages, instead of just English using ASCII

    echo 'fred nerk' | perl -e 'undef $/;$a=<>; $a=~s/\b(.)/\u$1/sg; print $a';

    What the above does is...

    undef $/; # treats line breaks like spaces, instead of input terminators
    $a=<>; # Gets the input
    $a=~s/\b(.)/\u$1/sg; # Does the conversion*
    print $a; # Outputs it

    Here's the regular expression explanation:-

    $a      # This has out input
    =~     # Tells perl to convert it using the following regexp
    s/       # Says we want to substiture stuff
    \b(.)   # Matches any "word break" and grabs the next character
    /        # End of the match separator
    \u$1   # replaces what we matched with the uppercase version of it ($1 is the variable from the earlier matching brackets)
    /sg;   # "s" means treat line breaks like spaces, "g" means do everything (not just the 1st)
    LVL 2

    Expert Comment

    p.s. beware of things already containing quotes, apostrophies, and backtics - many people enjoy putting their names into web forms by typing things like:-

    joe `rm -rf .` smith

    and so on - which of course will erase your entire PC if you run it through any script without noticing :-)

    Also - there's a difference between " and ' under unix: you probably meant ' and not " earlier - do this to see why:-
    echo "fred $HOME"
    echo "fred `ls`"

    Author Comment


    Thanks Chris,

    I took my sweet time getting back to this, but that was a great answer, and some very useful hints hacked on at the end too. Apologies for taking so long, but the help and information is appreciated.


    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    rdate is a Linux command and the network time protocol for immediate date and time setup from another machine. The clocks are synchronized by entering rdate with the -s switch (command without switch just checks the time but does not set anything). …
    Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
    Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now