检查环境变量是否已设置[复制]

时间:2022-05-18 11:43:18

This question already has an answer here:

这个问题已经有了答案:

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脚本,是否已经设置了环境变量?

4 个解决方案

#1


28  

[ -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 VARIABLE but it was not exported. That means that it is not an environment variable at all.

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

#2


47  

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:

只有在变量为unset时,才会将VAR设置为值“foo”。若要将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 env will not work. Consider:

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

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

Nor does it work to spawn a subshell and test:

它也不能产生一个亚壳层和测试:

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.

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

#3


15  

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.

如果变量没有设置,右边将等于abc。注意,内部操作符:=可以替换为:-测试变量是否未设置或为空。

#4


7  

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

This checks if the length of $VARIABLE is zero.

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

#1


28  

[ -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 VARIABLE but it was not exported. That means that it is not an environment variable at all.

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

#2


47  

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:

只有在变量为unset时,才会将VAR设置为值“foo”。若要将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 env will not work. Consider:

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

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

Nor does it work to spawn a subshell and test:

它也不能产生一个亚壳层和测试:

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.

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

#3


15  

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.

如果变量没有设置,右边将等于abc。注意,内部操作符:=可以替换为:-测试变量是否未设置或为空。

#4


7  

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

This checks if the length of $VARIABLE is zero.

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