0

Say I have an env variable:

export foo="bar";
declare z="\$foo";

I want to echo the value of "$z", which should be "bar". But if I run:

 echo "$z"

I get this literal:

$foo

and if I run this instead:

echo "$(eval "$z")"

I get bar is not a command.

But instead I am looking to just get

"bar"

as the echoed result. How can I do that?

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
  • Sounds like what you want is indirect expansion. See [BashFAQ #6](https://mywiki.wooledge.org/BashFAQ/006#Evaluating_indirect.2Freference_variables) – Charles Duffy Jul 06 '18 at 01:00
  • (I've updated the link to point more directly to the part of the FAQ that's on-topic for your question, if you followed it earlier). – Charles Duffy Jul 06 '18 at 01:02
  • If you run `foo=bar; bar=baz`, then `echo "$foo"` will emit `bar`, but `echo "${!foo}"` will emit `baz`. – Charles Duffy Jul 06 '18 at 01:03
  • Note that you need to put `lmx_setting_a`, not `$lmx_setting_a`, inside your variable; the `$` is a sigil that tells the shell to do parameter expansion, not part of the variable name itself. – Charles Duffy Jul 06 '18 at 01:04
  • already closed-as-dupe -- so the question is serving as a guidepost to prior instances; no need for it to have its own answer when those answers already exist on the prior copies. – Charles Duffy Jul 06 '18 at 01:07

0 Answers0