17

I am reading a character from keyboard and converting it to uppercase and then displaying the character again.

But the following code produces an error:

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
Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
avinashse
  • 1,440
  • 4
  • 30
  • 55

7 Answers7

36

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
P.P
  • 117,907
  • 20
  • 175
  • 238
  • This works for some languages but not all. See https://stackoverflow.com/a/68455313/289099 for a more widely-applicable solution. Specifically, use `tr` like so: `tr [:lower:] [:upper:];` – pattivacek Jun 20 '22 at 08:21
29

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.

See also: Bash parameter expansion.

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
dogbane
  • 266,786
  • 75
  • 396
  • 414
  • 3
    I think OP never mentioned what shell is being used. – axiom Dec 04 '12 at 10:59
  • 1
    Decrementing because doesn't work:# a="${a^^}" -bash: ${a^^}: bad substitution – Kevin Buchs Dec 16 '15 at 02:42
  • How did you start `bash`, @KevinBuchs? It works here, but it doesn't work with `sh`, for example. Also, what is the content of the variable `a` at that point? For me, this is the most elegant solution, as it avoids forking further processes. – Ulrich Eckhardt Apr 12 '16 at 12:18
  • 4
    The `^^` parameter expansion was added in Bash 4.0. – nisetama May 07 '16 at 00:41
9

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

echo "öäüßè" | awk 'BEGIN { getline; print toupper($0) }'
Juergen H.
  • 91
  • 1
  • 1
6

Use command substitution:

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

Note the ticks ` around echo and tr.

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
axiom
  • 8,765
  • 3
  • 36
  • 38
  • You made a similar remark above yourself, but here you assume that the shell is `bash` (or sufficiently similar), too. This doesn't work with `fish`, for example. – Ulrich Eckhardt Apr 12 '16 at 12:20
4

using a bash script

printf "Type your Message Here: " read message

echo Upper Case: $message | tr [:lower:] [:upper:];
echo Lower Case: $message | tr [:upper:] [:lower:]
buddemat
  • 4,552
  • 14
  • 29
  • 49
1

awk is the wrong way to go here, but here's one way it could be done:

a=$(awk 'BEGIN { getline; print toupper($0) }')
echo $a
Thor
  • 45,082
  • 11
  • 119
  • 130
0

Could not get

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

to work, but

a=`echo $a | tr '[a-z]' '[A-Z]'`

did (note additional regex [] brackets.
Within a /usr/bin/sh script this worked as in

...
while getopts ":l:c:" option; do  
   case "$option"  
   in  
      l) L_OPT=`echo ${OPTARG}| tr '[a-z]' '[A-Z]'`  
         ;;  
      c) C_OPT=`echo ${OPTARG} | tr '[a-z]' [A-Z]'`  
         ;;  
      \?)  
         echo $USAGE  
         exit 1  
         ;;  
   esac  
done  
...
Andrew
  • 11
  • 3