Linux 检查环境变量是否已经设置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11686208/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 13:56:32  来源:igfitidea点击:

check if environment variable is already set

linuxshellunix

提问by Lolly

I am writing a shell script, where I have to check if environment variable is set, if not set then I have to set it. Is there any way to check in shell script, whether an environment variable is already set or not ?

我正在编写一个shell脚本,我必须检查是否设置了环境变量,如果未设置,则必须设置它。有没有办法检查shell脚本,是否已经设置了环境变量?

采纳答案by Igor Chubin

[ -z "$VARIABLE" ] && VARIABLE="abc"

if env | grep -q ^VARIABLE=
then
  echo env variable is already exported
else
  echo env variable was not exported, but now it is
  export VARIABLE
fi

I want to stress that [ -z $VARIABLE ]is not enough, because you can have VARIABLEbut it was not exported. That means that it is not an environment variable at all.

我想强调这[ -z $VARIABLE ]还不够,因为你可以拥有VARIABLE但没有出口。这意味着它根本不是环境变量。

回答by unutbu

if [ -z "$VARIABLE" ]; then
    VARIABLE=...
fi

This checks if the length of $VARIABLE is zero.

这将检查 $VARIABLE 的长度是否为零。

回答by Bentoy13

What you want to do is native in bash, it is called parameter substitution:

你想要做的是bash中的原生,它被称为参数替换:

VARIABLE="${VARIABLE:=abc}"

If VARIABLE is not set, right hand side will be equal to abc. Note that the internal operator :=may be replaced with :-which tests if VARIABLE is not set or empty.

如果未设置 VARIABLE,右侧将等于 abc。请注意,如果 VARIABLE 未设置或为空,则内部运算符:=可能会替换为:-which 测试。

回答by William Pursell

The standard solution to conditionally assign a variable (whether in the environment or not) is:

有条件地分配变量(无论是否在环境中)的标准解决方案是:

: ${VAR=foo}

That will set VAR to the value "foo" only if it is unset.
To set VAR to "foo" if VAR is unset or the empty string, use:

只有在未设置时,才会将 VAR 设置为值“foo”。
如果 VAR 未设置或为空字符串,要将 VAR 设置为“foo”,请使用:

: ${VAR:=foo}

To put VAR in the environment, follow up with:

要将 VAR 放入环境中,请跟进:

export VAR

You can also do export VAR=${VAR-foo}or export VAR=${VAR:=foo}, but some older shells do not support the syntax of assignment and export in the same line. Also, DRY; using the name on both sides of the =operator is unnecessary repetition. (A second line exporting the variable violates the same principal, but feels better.)

您也可以执行export VAR=${VAR-foo}export VAR=${VAR:=foo},但某些较旧的 shell 不支持同一行中的赋值和导出语法。此外,; 在=运算符的两边使用名称是不必要的重复。(导出变量的第二行违反了相同的原则,但感觉更好。)

Note that it is very difficult in general to determine if a variable is in the environment. Parsing the output of envwill not work. Consider:

请注意,通常很难确定变量是否在环境中。解析 的输出env将不起作用。考虑:

export foo='
VAR=var-value'
env | grep VAR

Nor does it work to spawn a subshell and test:

生成子shell并进行测试也不起作用:

sh -c 'echo $VAR'

That would indicate the VAR is set in the subshell, which would be an indicator that VAR is in the environment of the current process, but it may simply be that VAR is set in the initialization of the subshell. Functionally, however, the result is the same as if VAR is in the environment. Fortunately, you do not usually care if VAR is in the environment or not. If you need it there, put it there. If you need it out, take it out.

这将表明 VAR 是在子 shell 中设置的,这将表明 VAR 在当前进程的环境中,但它可能只是在子 shell 的初始化中设置了 VAR。然而,在功能上,结果与 VAR 在环境中是一样的。幸运的是,您通常不关心 VAR 是否在环境中。如果你需要它,把它放在那里。如果你需要它,把它拿出来。