Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert to Uppercase in shell

I am reading a character from keyboard and converting it to uppercase and then displaying the character again. But this is showing error. How can I do this.

my code:-

read a;
a=echo $a | tr 'a-z' 'A-Z'
echo $a

I also tried this :-

read option;
eval $(awk -v option=$option '{print "a="toupper(option);}')
echo $a
like image 813
avinashse Avatar asked Dec 04 '12 10:12

avinashse


2 Answers

If you want to store the result of a back in a, then you can do use command substitution:

read a;
a=$(echo $a | tr 'a-z' 'A-Z')
echo $a
like image 97
P.P Avatar answered Oct 08 '22 02:10

P.P


This can be done natively in bash as follows:

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

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

like image 39
dogbane Avatar answered Oct 08 '22 02:10

dogbane