Convert to uppercase in shell

I read the character from the keyboard and convert it to uppercase, and then show the character again. But it shows an error. How can i do this.

my code is: -

read a; a=echo $a | tr 'az' 'AZ' echo $a 

I also tried this: -

 read option; eval $(awk -v option=$option '{print "a="toupper(option);}') echo $a 
+4
source share
6 answers

If you want to save the result of a to a , then you can use command substitution:

 read a; a=$(echo $a | tr 'az' 'A-Z') echo $a 
+12
source

This can be done initially in bash as follows:

 read a; a="${a^^}" echo "$a" 

There is no need to invoke other commands, such as tr , because bash can do this by itself.

+14
source

AWK is the right way to convert upper / lower case with full Unicode support; -)

 echo "âÀüßè" | awk 'BEGIN { getline; print toupper($0) }' 
+5
source

Use command substitution:

 a=`echo $a | tr 'az' 'A-Z'` 

Pay attention to the tics `around the echo and tr.

+2
source

awk is the wrong way, but there is one way to do it:

 a=$(awk 'BEGIN { getline; print toupper($0) }') echo $a 
+1
source

Failed to get

 a=`echo $a | tr 'az' 'A-Z'` 

but

 a=`echo $a | tr '[az]' '[AZ]'` 

did (note the extra brackets regex [].
Inside the / usr / bin / sh script this worked like

 ... while getopts ":l:c:" option; do case "$option" in l) L_OPT=`echo ${OPTARG}| tr '[az]' '[AZ]'` ;; c) C_OPT=`echo ${OPTARG} | tr '[az]' [AZ]'` ;; \?) echo $USAGE exit 1 ;; esac done ... 
0
source

All Articles