Convert characters to upper case to lower case and vice versa

I tried to convert lowercase to uppercase. I came across various alternatives, for example, a single list in the StackOverflow question . However, what I saw was that they were just printed. I want to save it to another variable that I can use later. Can anyone tell me how I can achieve this?

+4
source share
6 answers

Your entry is $a . New variable $b .
(borrowed from here , written by @ ghostdog74)

using tr :

 b=$( tr '[AZ]' '[az]' <<< $a) 

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

 set b=`echo "$a" | tr '[AZ]' '[az]'` 
+6
source

using bash 4.0 :

 b=${a,,} 
+3
source

using awk :

 b=$( awk '{print tolower($0)}' <<< $a ) 
+2
source

using perl :

 b=$( perl -e 'print lc <>;' <<< $a ) 
+2
source

I know this is an old post, but I made this answer for another site, so I thought I posted it here:

here comes the answer of the programmers ....

TOP -> bottom : use python:

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

Or Ruby:

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

Or Perl (maybe 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 one:

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

You can also use dd (but I would not!):

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

below → TOP :

use python:

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

Or Ruby:

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

Or Perl (maybe 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 one:

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

You can also use dd (but I would not!):

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

Also, when you say shell, I assume you mean bash , but if you can use zsh , it's as simple as

 b=$a:l 

for lowercase and

 b=$a:u 

for uppercase.

+1
source

All of the previous answers are correct, I just add this because there is no need to declare a variable, etc. if you are just converting texts.

 echo changethistoupper | tr [az] [AZ] echo CHANGETHISTOLOWER | tr [AZ] [az] 

enter image description here

0
source

Source: https://habr.com/ru/post/1392890/


All Articles