Saturday, September 30, 2023
2
rated 0 times [  2] [ 0]  / answers: 1 / hits: 1226  / 2 Years ago, fri, december 3, 2021, 2:50:09

I simulated the situation where the value of variable bar is $foo. foo is a variable too, which has the value hello.


$ foo=hello
$ bar=$(echo "$foo")
$ echo $bar
$foo

How I can get to hello, given foo="hello" and bar="$foo"?


I tried:


$ echo $(echo $bar)

But the output still is:


$foo

More From » command-line

 Answers
7

Use eval like this:


$ foo=hello
$ bar=$(echo "$foo")
$ echo "$bar"
$foo
$ eval echo "$bar"
hello
$ foo=world
$ eval echo "$bar"
world

In the above example, the eval bash builtin command will parse the arguments once more, so that the $bar variable contents will be interpreted once more as a variable ($foo) and its current contents (hello in the first case and world in the second case) will be used as the argument to the echo command.


[#1514] Friday, December 3, 2021, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
biryanrownies

Total Points: 396
Total Questions: 90
Total Answers: 106

Location: Saint Lucia
Member since Sun, Sep 5, 2021
2 Years ago
biryanrownies questions
Wed, Sep 7, 22, 18:13, 1 Year ago
Sat, Feb 12, 22, 16:02, 2 Years ago
Sat, Apr 15, 23, 09:22, 6 Months ago
;