|
Home > Archive > Unix Shell > September 2005 > Evaluate a variable that is inside a string variable
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Evaluate a variable that is inside a string variable
|
|
|
| Hi,
I have long string, which ia a variable with the content having another
variable.
Ex. myStr="This is a long string with ${var} as value of embedded
variable ...."
I want myStr to print the above line with ${var} replaced with its
value.
ie, If I set my ${var} to VALUE1 and echo the myStr, I want the value
to be
var=VALUE_1
echo ${myStr}
--I want to have the output as below. Currently, I have myStr echoes
whatever I set at the beginning (ie, without the $var1 getting
evaluated).
This is a long string with VALUE_1 as value of embedded variable ....
var=VALUE_2
echo ${myStr}
This is a long string with VALUE_2 as value of embedded variable ....
Is there a way I can get this working? I tried with eval, but I guess I
am not using it correctly
Thanks.
| |
| Benjamin Schieder 2005-09-20, 6:05 pm |
| da wrote:
> Ex. myStr="This is a long string with ${var} as value of embedded
> variable ...."
>
> I want myStr to print the above line with ${var} replaced with its
> value.
>
> ie, If I set my ${var} to VALUE1 and echo the myStr, I want the value
> to be
eval echo ${myStr}
That should do it.
Greetings,
Benjamin
--
Benjamin 'blindCoder' Schieder
Registered Linux User #289529: http://counter.li.org
finger blindcoder@scavenger.homeip.net | gpg --import
--
/lusr/bin/brain: received signal: SIGIDIOT
| |
| Chris F.A. Johnson 2005-09-20, 6:05 pm |
| On 2005-09-20, da wrote:
> Hi,
>
> I have long string, which ia a variable with the content having another
> variable.
>
> Ex. myStr="This is a long string with ${var} as value of embedded
> variable ...."
>
> I want myStr to print the above line with ${var} replaced with its
> value.
>
> ie, If I set my ${var} to VALUE1 and echo the myStr, I want the value
> to be
>
> var=VALUE_1
> echo ${myStr}
> --I want to have the output as below. Currently, I have myStr echoes
> whatever I set at the beginning (ie, without the $var1 getting
> evaluated).
> This is a long string with VALUE_1 as value of embedded variable ....
>
> var=VALUE_2
> echo ${myStr}
> This is a long string with VALUE_2 as value of embedded variable ....
>
> Is there a way I can get this working? I tried with eval, but I guess I
> am not using it correctly
Always post *exactly* what you tried.
In the assignment to myStr, the variable is already expanded. If it
had a value, it would already be in $myStr; if not, ${var} would be
replaced by nothing. You need to escape the dollar sign:
myStr="This is a long string with \${var} as value of embedded variable ...."
var=VALUE_1
eval "echo ${myStr}"
--
Chris F.A. Johnson <http://cfaj.freeshell.org>
========================================
==========================
Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
<http://www.torfree.net/~chris/books/cfaj/ssr.html>
| |
|
| I forgot to mention, I am using "ksh"
| |
|
| Sorry, I will include the steps I tried in the future posts.
I had the script exactly same as Chris', but with a major error. I did
not have the "$" protected (escaped) in the ${var}.
Now it works perfect.
Thank you!
| |
| Chris F.A. Johnson 2005-09-20, 6:05 pm |
| On 2005-09-20, da wrote:
> Sorry, I will include the steps I tried in the future posts.
Please quote enough of the post you are replying to to give the
context of your own post. This is usenet, not a web forum (though
it is also bastardized on several web sites). I cannot see the
previous posts unless you quote them.
When using groups.google.com to reply to a Usenet article, click
on "show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers. This will quote the
previous message in the accepted manner. Trim the parts of it that
are not relevant to your follow-up.
> I had the script exactly same as Chris', but with a major error. I did
> not have the "$" protected (escaped) in the ${var}.
>
> Now it works perfect.
--
Chris F.A. Johnson <http://cfaj.freeshell.org>
========================================
==========================
Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
<http://www.torfree.net/~chris/books/cfaj/ssr.html>
|
|
|
|
|