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?

5
  • Sounds like what you want is indirect expansion. See BashFAQ #6 Commented Jul 6, 2018 at 1: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). Commented Jul 6, 2018 at 1:02
  • If you run foo=bar; bar=baz, then echo "$foo" will emit bar, but echo "${!foo}" will emit baz. Commented Jul 6, 2018 at 1: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. Commented Jul 6, 2018 at 1: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. Commented Jul 6, 2018 at 1:07

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.