Operating System - HP-UX
1819809 Members
2737 Online
109607 Solutions
New Discussion юеВ

Re: Changing a string to lower cases in korn shell script

 
SOLVED
Go to solution

Changing a string to lower cases in korn shell script

Hello,
I want to change the value stored in a string variable to all lower case.
For instance :
VAR1=AbcD then I want this variable becomes :
VAR1=abcd
How can I do that ?
Thanks in advance for your help.
Laurent MENEGUZY
8 REPLIES 8
Stefan Farrelly
Honored Contributor

Re: Changing a string to lower cases in korn shell script

echo test|tr "[:lower:]" "[:upper:]"
Im from Palmerston North, New Zealand, but somehow ended up in London...
Pete Randall
Outstanding Contributor

Re: Changing a string to lower cases in korn shell script

echo $VAR1 |tr "[:upper:]" "[:lower:]"


Pete

Pete
Bill McNAMARA_1
Honored Contributor

Re: Changing a string to lower cases in korn shell script

tr -s '[:upper:]' '[:lower:]'




It works for me (tm)
Stefan Farrelly
Honored Contributor
Solution

Re: Changing a string to lower cases in korn shell script


VAR1=$(echo $VAR1|tr "[:upper:]" "[:lower:]")
Im from Palmerston North, New Zealand, but somehow ended up in London...
Paula J Frazer-Campbell
Honored Contributor

Re: Changing a string to lower cases in korn shell script

Hi

Varable | tr -s "[A-Z]" "[a-z]"

Paula
If you can spell SysAdmin then you is one - anon
Francisco J. Soler
Honored Contributor

Re: Changing a string to lower cases in korn shell script

The ideas are all good but, but a more complete solution to your problem could be:

VAR1=`echo $VAR1 | tr -s "[A-Z]" "[a-z]"`

Be careful wit the quotation.

Frank
Linux?. Yes, of course.
Bill Hassell
Honored Contributor

Re: Changing a string to lower cases in korn shell script

It's much more efficient to let the shell do the work rather than a separate process:


typeset -l VAR2

VAR1=AbcD
VAR2="$VAR1"
echo $VAR1 $VAR2
AbcD abcd

When assigning strings, it's always a good idea to use double quotes so any imbedded spaces, etc, are preserved. The typeset attribute has more than a dozen formatting options such as UPPERCASE, lowewrcase, left/right justified, zero-fill, etc. typeset is available in POSIX shells such as the HP-UX POSIX shell and ksh as well as BASH.


Bill Hassell, sysadmin

Re: Changing a string to lower cases in korn shell script

Thanks for your help. It's working fine.
Laurent MENEGUZY