2

I tried to convert a lowercase string to uppercase and assign it to a variable using the following code

The script is written in .tn extension

set y a12
y_up=$( tr '[A-Z]' '[a-z]' <<< $y)
echo $y
echo $y_up

But I am getting the error

invalid command name "A-Z"
while executing
"A-Z"
invoked from within
"y_up=$( tr '[A-Z]' '[a-z]' <<< $y) "

How can I convert this?

user3304726
  • 209
  • 2
  • 4
  • 17

3 Answers3

7

Below Works, Try this.

bash-3.2$echo lower to upper | tr '[:lower:]' '[:upper:]'
LOWER TO UPPER  

# To Save in the variable use below
var=$(echo lower to upper | tr '[:lower:]' '[:upper:]')
Fidel
  • 927
  • 1
  • 6
  • 13
4

BASH 4+ version has native way to convert sting to upper case:

upperStr="${str^^}"
anubhava
  • 713,503
  • 59
  • 514
  • 593
2

This should work:

$ y="Foo Bar Baz"
$ y_up=$(tr '[A-Z]' '[a-z]' <<< $y)
$ echo $y_up
foo bar baz