bash: convert uppercase to lowecase

bash: convert uppercase to lowecase

Post by Rob » Fri, 25 Mar 2005 01:01:05



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!

 
 
 

bash: convert uppercase to lowecase

Post by Chris F.A. Johnso » Fri, 25 Mar 2005 01:06:17



> 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

 
 
 

bash: convert uppercase to lowecase

Post by Rob » Fri, 25 Mar 2005 01:20:03




>> 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' )

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