6

I was trying to convert the lowercase characters to uppercase. I came across various alternatives like one listing at the StackOverflow question. However, What i saw that these are just printed. I want to save it to another variable which i can use later. Can anyone tell how i can achieve this?

Community
  • 1
  • 1
Abhinav
  • 1,496
  • 3
  • 15
  • 31

6 Answers6

9

I know this is an oldish post but I made this answer for another site so I thought I'd post it up here:

here comes a programmers answer....

UPPER -> lower: use python:

b=`echo "print '$a'.lower()" | python`

Or Ruby:

b=`echo "print '$a'.downcase" | ruby`

Or Perl (probably my favorite):

b=`perl -e "print lc('$a');"`

Or PHP:

b=`php -r "print strtolower('$a');"`

Or Awk:

b=`echo "$a" | awk '{ print tolower($1) }'`

Or Sed:

b=`echo "$a" | sed 's/./\L&/g'`

Or Bash 4:

b=${a,,}

Or NodeJS if you have it:

b=`echo "console.log('$a'.toLowerCase());" | node`

You could also use dd (but I wouldn't!):

b=`echo "$a" | dd  conv=lcase 2> /dev/null`

lower -> UPPER:

use python:

b=`echo "print '$a'.upeer()" | python`

Or Ruby:

b=`echo "print '$a'.upcase" | ruby`

Or Perl (probably my favorite):

b=`perl -e "print uc('$a');"`

Or PHP:

b=`php -r "print strtoupper('$a');"`

Or Awk:

b=`echo "$a" | awk '{ print toupper($1) }'`

Or Sed:

b=`echo "$a" | sed 's/./\U&/g'`

Or Bash 4:

b=${a^^}

Or NodeJS if you have it:

b=`echo "console.log('$a'.toUpperCase());" | node`

You could also use dd (but I wouldn't!):

b=`echo "$a" | dd  conv=ucase 2> /dev/null`

Also when you say 'shell' I'm assuming you mean bash but if you can use zsh it's as easy as

b=$a:l

for lower case and

b=$a:u

for upper case.

nettux
  • 5,270
  • 2
  • 23
  • 33
  • how about capitalize the first letter on `zsh`, do you have any reference for these commands? @nettux – mochadwi Dec 08 '19 at 16:02
  • 1
    @mochadwi You'd use `b=${(C)a}`. See the parameter expansion section from the zsh docs here: http://zsh.sourceforge.net/Doc/Release/Expansion.html#Parameter-Expansion-Flags – nettux Dec 18 '19 at 15:40
7

Your input is $a. The new variable is $b.
(borrowed from here written by @ghostdog74)

using tr:

b=$( tr '[A-Z]' '[a-z]' <<< $a)

if you use tcsh, then use echo instead of <<<:

set b=`echo "$a" | tr '[A-Z]' '[a-z]'`
Community
  • 1
  • 1
oHo
  • 51,447
  • 27
  • 165
  • 200
  • 1
    You should post these as separated answers :) – khachik Jan 25 '12 at 10:23
  • Is that for tcsh? Consider the following as you mentioned gives error.
    
    #! /bin/tcsh  
    set a = "Helo World!"  
    echo $a '<-'
    set b = $(tr '[A-Z]' '[a-z]' <<< $a )  # error
    echo 'a=' $a 'b=' $b  
    
    
    The shell gives me error : "Illegal variable name." at line 4
    – Abhinav Jan 25 '12 at 12:28
  • 1
    Hey @abhinav. I have fixed the `tcsh` version. I am sorry for the late, not so familiar with `tcsh`. Have fun ;-) – oHo Jan 25 '12 at 15:39
  • yea, having multiple separate answers (especially 1-liners) is discouraged. Best to have them all in one answer – SiegeX Jan 25 '12 at 17:11
4

using bash 4.0:

b=${a,,}
oHo
  • 51,447
  • 27
  • 165
  • 200
2

using awk:

b=$( awk '{print tolower($0)}' <<< $a )
oHo
  • 51,447
  • 27
  • 165
  • 200
2

using perl:

b=$( perl -e 'print lc <>;' <<< $a )
oHo
  • 51,447
  • 27
  • 165
  • 200
0

All the previous answers are correct, I'm just adding this because there is no need to declare variable etc if you are simply converting texts.

echo changethistoupper | tr [a-z] [A-Z]
echo CHANGETHISTOLOWER | tr [A-Z] [a-z]

enter image description here

grepit
  • 21,260
  • 6
  • 105
  • 81