[英]Defining environment variables with another variable
For long and boring reasons, I need to retrieve one environment variable based on the value of another.由于冗长而无聊的原因,我需要根据另一个环境变量的值来检索一个环境变量。 eg例如
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?我怎样才能像这样在 Bash 中得到值/usr/bin/blah
回显?
Of course after finding nothing on google for ages, I find the answer 3 seconds after posting my question. 当然,在Google上搜索了很长时间之后,我在发布问题3秒钟后才找到答案。 You do: 你做:
> echo ${!LOG_PROCESSOR}
> /usr/bin/logcourier
export LOGCOURIER=/usr/bin/blah
export LOG_PROCESSOR=$LOGCOURIER
echo ${LOG_PROCESSOR}
In addition to indirect parameter expansion, bash
4.3 will have named references. 除了间接参数扩展之外, bash
4.3将具有命名引用。
declare -n LOG_PROCESSOR=LOGCOURIER
After this declaration, a change to one variable results in a change to the other. 声明之后,对一个变量的更改将导致对另一个变量的更改。
If you have access to Python, this can be done like so:如果您有权访问 Python,则可以这样做:
export LOG_PROCESSOR=LOGCOURIER
export LOGCOURIER=/usr/bin/blah
echo $(python -c "import os; print(os.environ[os.environ['LOG_PROCESSOR']])")
> /usr/bin/blah
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.