5

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?

1
  • 1
    set does not what you think it does... Commented Feb 13, 2014 at 6:25

3 Answers 3

10

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:]')
1
  • Actually I am writing these in .tn script , there it is still showing the error that :lower: command not found Commented Feb 13, 2014 at 6:50
10

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

str="Some string here"
upperStr="${str^^}"
2

This should work:

$ y="Foo Bar Baz"
$ y_up=$(tr '[A-Z]' '[a-z]' <<< $y)
$ echo $y_up
foo bar baz
1
  • This is an old answer, but it actually does the opposite of what is asked in the question: it converts uppercase letters to lowercase. The tr command should look like: tr '[a-z]' '[A-Z]'
    – mathieures
    Commented Apr 22 at 15:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.