1

For long and boring reasons, I need to retrieve one environment variable based on the value of another. e.g.

export LOG_PROCESSOR=LOGCOURIER
export LOGCOURIER=/usr/bin/blah
echo  ${$LOG_PROCESSOR}
-bash: ${$LOG_PROCESSOR}: bad substitution

How can I get the value /usr/bin/blah echo'd in Bash like this?

4 Answers 4

4

Of course after finding nothing on google for ages, I find the answer 3 seconds after posting my question. You do:

 > echo ${!LOG_PROCESSOR}
 > /usr/bin/logcourier
Sign up to request clarification or add additional context in comments.

Comments

0
export LOGCOURIER=/usr/bin/blah
export LOG_PROCESSOR=$LOGCOURIER
echo  ${LOG_PROCESSOR}

Comments

0

In addition to indirect parameter expansion, bash 4.3 will have named references.

declare -n LOG_PROCESSOR=LOGCOURIER

After this declaration, a change to one variable results in a change to the other.

Comments

-1

If you have access to Python, this can be done like so:

export LOG_PROCESSOR=LOGCOURIER
export LOGCOURIER=/usr/bin/blah
echo $(python -c "import os; print(os.environ[os.environ['LOG_PROCESSOR']])")
> /usr/bin/blah

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.