0

Say I have an env variable:

  echo lmx_setting_foo="bar";

the above env variable was created like:

$ lmx set foo bar

and that did:

export "lmx_setting_$2"="$3"

Say someone wants to retrieve "foo", they would do:

$ lmx get foo

but if I do this:

echo "$lmx_setting_$2"

it doesn't seem to work

2

There's a few ways to go about this, but the most portable is probably

eval "echo \$lmx_setting_$2"

but the bash-specific way to avoid an eval would be

temp="lmx_setting_$2"
echo ${!temp}

If you haven't before, you should open the bash man page and scroll down to Parameter Expansion to see all your options. You specifically asked about environment variables, but if you don't need to export these variables and you have a new enough version of bash (4.0+) you can use Associative Arrays and avoid this whole mess ...but then your script really isn't portable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.