bash 如何检查环境变量是否存在并获取其值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39296472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if an environment variable exists and get its value?
提问by Nicolas El Khoury
I am writing a shell script. In this shell script, I am have a variable that either takes a default value, or the value of an environment variable. However, the environment variable doesn't have to be present.
我正在编写一个shell脚本。在这个 shell 脚本中,我有一个变量,它要么采用默认值,要么采用环境变量的值。但是,环境变量不必存在。
For instance, assume, before running the script, I perform the following operation:
例如,假设在运行脚本之前,我执行以下操作:
export DEPLOY_ENV=dev
How do I tell the script to search for this environment variable, and store its value in a variable inside the script. Moreover, how do I tell the script that if this environment variable does not exist, store a default variable?
我如何告诉脚本搜索此环境变量,并将其值存储在脚本内的变量中。此外,如何告诉脚本如果此环境变量不存在,则存储一个默认变量?
回答by endurogizer
[ -z "${DEPLOY_ENV}" ]
checks whether DEPLOY_ENV
has length equal to zero. So you could run:
[ -z "${DEPLOY_ENV}" ]
检查DEPLOY_ENV
长度是否为零。所以你可以运行:
if [[ -z "${DEPLOY_ENV}" ]]; then
MY_SCRIPT_VARIABLE="Some default value because DEPLOY_ENV is undefined"
else
MY_SCRIPT_VARIABLE="${DEPLOY_ENV}"
fi
# or using a short-hand version
[[ -z "${DEPLOY_ENV}" ]] && MyVar='default' || MyVar="${DEPLOY_ENV}"
# or even shorter use
MyVar="${DEPLOY_ENV:-default_value}"
回答by Eugene Yarmash
You could just use parameter expansion:
你可以只使用参数扩展:
${parameter:-word}
If parameteris unset or null, the expansion of wordis substituted. Otherwise, the value of parameteris substituted.
${参数:-word}
如果参数未设置或为空,则替换单词的扩展。否则,替换参数的值。
So try this:
所以试试这个:
var=${DEPLOY_ENV:-default_value}
There's also the ${parameter-word}form, which substitutes the default value only when parameteris unset (but not when it's null).
还有${parameter-word}形式,它仅在参数未设置时(但不为空时)才替换默认值。
To demonstrate the difference between the two:
为了证明两者之间的区别:
$ unset DEPLOY_ENV
$ echo "'${DEPLOY_ENV:-default_value}' '${DEPLOY_ENV-default_value}'"
'default_value' 'default_value'
$ DEPLOY_ENV=
$ echo "'${DEPLOY_ENV:-default_value}' '${DEPLOY_ENV-default_value}'"
'default_value' ''
回答by chepner
If you don't care about the difference between an unset variable or a variable with an empty value, you can use the default-value parameter expansion:
如果您不关心未设置变量或具有空值的变量之间的区别,则可以使用默认值参数扩展:
foo=${DEPLOY_ENV:-default}
If you docare about the difference, drop the colon
如果您确实关心差异,请删除冒号
foo=${DEPLOY_ENV-default}
You can also use the -v
operator to explicitly test if a parameter is set.
您还可以使用-v
运算符显式测试是否设置了参数。
if [[ ! -v DEPLOY_ENV ]]; then
echo "DEPLOY_ENV is not set"
elif [[ -z "$DEPLOY_ENV" ]]; then
echo "DEPLOY_ENV is set to the empty string"
else
echo "DEPLOY_ENV has the value: $DEPLOY_ENV"
fi
回答by Robert
There is no difference between environment variables and variables in a script. Environment variables are just defined earlier, outside the script, before the script is called. From the script's point of view, a variable is a variable.
环境变量和脚本中的变量没有区别。环境变量只是在调用脚本之前在脚本之外定义的。从脚本的角度来看,变量就是变量。
You can check if a variable is defined:
您可以检查是否定义了变量:
if [ -z "$a" ]
then
echo "not defined"
else
echo "defined"
fi
and then set a default value for undefined variables or do something else.
然后为未定义的变量设置默认值或做其他事情。
The -z
checks for a zero-length (i.e. empty) string. See man bash
and look for the CONDITIONAL EXPRESSIONS section.
在-z
一个零长度的检查(即空的)的字符串。查看man bash
并查找 CONDITIONAL EXPRESSIONS 部分。
You can also use set -u
at the beginning of your script to make it fail once it encounters an undefined variable, if you want to avoid having an undefined variable breaking things in creative ways.
set -u
如果您想避免未定义的变量以创造性的方式破坏事物,您还可以在脚本的开头使用,使其在遇到未定义的变量时失败。
回答by Preethi Srinivasan
NEW_VAR=""
if [[ ${ENV_VAR} && ${ENV_VAR-x} ]]; then
NEW_VAR=${ENV_VAR}
else
NEW_VAR="new value"
fi
回答by Nicolas El Khoury
All the answers worked. However, I had to add the variables that I needed to get to the sudoers files as follows:
所有的答案都有效。但是,我必须添加获取 sudoers 文件所需的变量,如下所示:
sudo visudo
Defaults env_keep += "<var1>, <var2>, ..., <varn>"