Uppercase a Bash input variable

Gerald Combs gerald at ethereal.com
Wed Apr 2 19:19:40 CST 2003


On Wed, 2 Apr 2003, Parker, Ron wrote:

> > -----Original Message-----
> > From: James Sissel [mailto:James.Sissel at labone.com]
> 
> > echo -n "Please enter a date (DD-MON-YYYY): "
> > read indate
> 
> > How can I make sure indate is in uppercase?
> 
> I'm sure there is a better answer, but something like
> 	indate="`echo $indate|tr [a-z] [A-Z]`"
> should work

You need to quote the ranges you feed to 'tr', e.g.

    indate="`echo $indate|tr '[a-z]' '[A-Z]'`"

Otherwise the shell will try to match against the two ranges.  GNU 'tr'
also lets you use

    indate="`echo $indate|tr '[:lower:]' '[:upper:]'`"

You could also allow the user to enter a more free-form date (e.g. "April
15, 2003" or "4/15/03") and convert it yourself:

    echo -n "Please enter a date: "
    read rawdate

    indate=`date -d "$rawdate" +"%d-%b-%Y" | tr '[a-z]' '[A-Z]'`

The 'date' command (assuming you're using the GNU version) takes the
user-supplied string and converts it to the required format.  You still
need to translate lower to upper case, since 'date' outputs mixed case.




More information about the Kclug mailing list