setting an output to a variable in unix

November 13, 2009 at 07:35:55
Specs: Windows XP
I want to translate a variable and read the output to another variable

echo $my_var | tr '0123456789' 'ZZZZZZZZZZ'

works as I'd expect, printing the correctly translated output. But how do I assign the output to a variable.

I wanted to do something like

my_new_var = `$my_var | tr '0123456789' 'ZZZZZZZZZZ'`

but my computer is saying no.

Any help greatly appreciated

See More: setting an output to a variable in unix

Report •

November 13, 2009 at 08:10:31
Well, my system says yes, so you're going to have to give a bit more information.

Report •

November 13, 2009 at 10:01:33
Yes, more information is needed like what shell you are using. This works on my Solaris 9 box:


my_var="hi 234"
my_new_var=`echo "$my_var" | tr '0123456789' 'ZZZZZZZZZZ'`
echo "$my_new_var"
# end script

Some observations:
1) you forgot the echo command
2) get rid of the white space around the = sign
3) If the my_var variable contains white space, surround the variable name with double quotes as shown.

Report •

Related Solutions

Ask Question