Unix Shell - bash: convert uppercase to lowecase

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > March 2005 > bash: convert uppercase to lowecase





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author bash: convert uppercase to lowecase
Rob

2005-03-30, 7:55 am

I need to convert strings in BASH to all lowercase. What have I done wrong
here?

string=TEST
string2="$string" | tr '[A-Z]' '[a-z]'
echo $string
echo $string2

i get a blank output for string2. thanks!


Chris F.A. Johnson

2005-03-30, 7:55 am

On Wed, 23 Mar 2005 at 16:01 GMT, Rob wrote:
> I need to convert strings in BASH to all lowercase. What have I done wrong
> here?
>
> string=TEST
> string2="$string" | tr '[A-Z]' '[a-z]'
> echo $string
> echo $string2
>
> i get a blank output for string2. thanks!


string2=$( printf "%s\n" "$string" | tr '[A-Z]' '[a-z]' )


Or, in a Bourne shell (works in bash, as well):

string2=`printf "%s\n" "$string" | tr '[A-Z]' '[a-z]'`

You probably don't need the square brackets (some old versions of
tr required them), so this should also work:

string2=$( printf "%s\n" "$string" | tr 'A-Z' 'a-z' )


--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
========================================
===========================
My code (if any) in this post is copyright 2005, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
Rob

2005-03-30, 7:55 am

Chris F.A. Johnson wrote:
> On Wed, 23 Mar 2005 at 16:01 GMT, Rob wrote:
>
> string2=$( printf "%s\n" "$string" | tr '[A-Z]' '[a-z]' )
>
>
> Or, in a Bourne shell (works in bash, as well):
>
> string2=`printf "%s\n" "$string" | tr '[A-Z]' '[a-z]'`
>
> You probably don't need the square brackets (some old versions of
> tr required them), so this should also work:
>
> string2=$( printf "%s\n" "$string" | tr 'A-Z' 'a-z' )


thanks, i had just figured it out
i had gone this route:
string=TEST
string2=`echo $string | tr A-Z a-z`
echo $string
echo $string2




Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com