在Unix shell脚本中,什么是检查环境变量的简洁方法?

时间:2021-11-04 12:12:59

I've got a few Unix shell scripts where I need to check that certain environment variables are set before I start doing stuff, so I do this sort of thing:

我有一些Unix shell脚本,在我开始做事情之前,我需要检查某些环境变量是否设置好,所以我要做这样的事情:

if [ -z "$STATE" ]; then    echo "Need to set STATE"    exit 1fi  if [ -z "$DEST" ]; then    echo "Need to set DEST"    exit 1fi

which is a lot of typing. Is there a more elegant idiom for checking that a set of environment variables is set?

这需要大量的输入。是否有一个更优雅的方法来检查一组环境变量的设置?

EDIT: I should mention that these variables have no meaningful default value - the script should error out if any are unset.

编辑:我应该指出,这些变量没有任何有意义的默认值——如果有的话,脚本应该会出错。

14 个解决方案

#1


500  

Parameter Expansion

The obvious answer is to use one of the special forms of parameter expansion:

最明显的答案是使用一种特殊形式的参数扩展:

: ${STATE?"Need to set STATE"}: ${DEST:?"Need to set DEST non-empty"}

Or, better (see section on 'Position of double quotes' below):

或者更好(见下面“双引号位置”一节):

: "${STATE?Need to set STATE}": "${DEST:?Need to set DEST non-empty}"

The first variant (using just ?) requires STATE to be set, but STATE="" (an empty string) is OK — not exactly what you want, but the alternative and older notation.

第一个变量(使用just ?)需要设置状态,但是STATE=""(空字符串)是可以的——不是您想要的,而是可选的和旧的符号。

The second variant (using :?) requires DEST to be set and non-empty.

第二个变体(使用:?)需要将DEST设置为非空。

If you supply no message, the shell provides a default message.

如果不提供消息,shell将提供一个默认消息。

The ${var?} construct is portable back to Version 7 UNIX and the Bourne Shell (1978 or thereabouts). The ${var:?} construct is slightly more recent: I think it was in System III UNIX circa 1981, but it may have been in PWB UNIX before that. It is therefore in the Korn Shell, and in the POSIX shells, including specifically Bash.

$ { var吗?}构造可以移植到第7版UNIX和Bourne Shell(1978)。$ { var:?}构造稍微更近一些:我认为它在1981年前后的System III UNIX中出现过,但在那之前它可能在PWB UNIX中出现过。因此,它在Korn Shell中,以及POSIX Shell中,包括特定的Bash。

It is usually documented in the shell's man page in a section called Parameter Expansion. For example, the bash manual says:

通常在shell的手册页中有一个名为参数展开的部分。例如,bash手册说:

${parameter:?word}

Display Error if Null or Unset. If parameter is null or unset, the expansion of word (or a message to that effect if word is not present) is written to the standard error and the shell, if it is not interactive, exits. Otherwise, the value of parameter is substituted.

如果为空或未设置,则显示错误。如果参数为空或未设置,则将word的扩展(或者如果没有word,则将其扩展为相应的消息)写入标准错误,如果非交互式,则将退出shell。否则,将替换参数的值。

The Colon Command

I should probably add that the colon command simply has its arguments evaluated and then succeeds. It is the original shell comment notation (before '#' to end of line). For a long time, Bourne shell scripts had a colon as the first character. The C Shell would read a script and use the first character to determine whether it was for the C Shell (a '#' hash) or the Bourne shell (a ':' colon). Then the kernel got in on the act and added support for '#!/path/to/program' and the Bourne shell got '#' comments, and the colon convention went by the wayside. But if you come across a script that starts with a colon, now you will know why.

我应该补充一句,冒号命令只需要对其参数进行评估,然后成功。它是最初的shell注释符号(在“#”到行尾之前)。很长一段时间,伯恩的shell脚本都有一个冒号作为第一个字符。C Shell将读取脚本并使用第一个字符来确定它是用于C Shell('#'哈希)还是Bourne Shell (a ':'冒号)。然后内核加入进来,添加了对“#!”的支持。/path/to/program'和Bourne shell得到了'#'注释,冒号约定被抛弃。但是如果您遇到一个以冒号开头的脚本,现在您就知道为什么了。


Position of double quotes

blong asked in a comment:

布隆评论道:

Any thoughts on this discussion? https://github.com/koalaman/shellcheck/issues/380#issuecomment-145872749

对这次讨论有什么想法吗?https://github.com/koalaman/shellcheck/issues/380 # issuecomment - 145872749

The gist of the discussion is:

讨论的要点是:

… However, when I shellcheck it (with version 0.4.1), I get this message:

但是,当我对它进行shell检查时(使用0.4.1版本),我得到如下信息:

In script.sh line 13:: ${FOO:?"The environment variable 'FOO' must be set and non-empty"}  ^-- SC2086: Double quote to prevent globbing and word splitting.

Any advice on what I should do in this case?

在这种情况下我该怎么做?

The short answer is "do as shellcheck suggests":

简短的回答是“照shellcheck建议做”:

: "${STATE?Need to set STATE}": "${DEST:?Need to set DEST non-empty}"

To illustrate why, study the following. Note that the : command doesn't echo its arguments (but the shell does evaluate the arguments). We want to see the arguments, so the code below uses printf "%s\n" in place of :.

为了说明原因,请学习以下内容。注意:命令不回显它的参数(但是shell会计算参数)。我们希望看到参数,因此下面的代码使用printf“%s\n”代替:。

$ mkdir junk$ cd junk$ > abc$ > def$ > ghi$ $ x="*"$ printf "%s\n" ${x:?You must set x}    # Careless; not recommendedabcdefghi$ unset x$ printf "%s\n" ${x:?You must set x}    # Careless; not recommendedbash: x: You must set x$ printf "%s\n" "${x:?You must set x}"  # Careful: should be usedbash: x: You must set x$ x="*"$ printf "%s\n" "${x:?You must set x}"  # Careful: should be used*$ printf "%s\n" ${x:?"You must set x"}  # Not quite careful enoughabcdefghi$ x=$ printf "%s\n" ${x:?"You must set x"}  # Not quite careful enoughbash: x: You must set x$ unset x$ printf "%s\n" ${x:?"You must set x"}  # Not quite careful enoughbash: x: You must set x$ 

Note how the value in $x is expanded to first * and then a list of file names when the overall expression is not in double quotes. This is what shellcheck is recommending should be fixed. I have not verified that it doesn't object to the form where the expression is enclosed in double quotes, but it is a reasonable assumption that it would be OK.

注意,当整个表达式不是双引号时,$x中的值如何扩展为first *,然后是文件名列表。这就是shellcheck建议要修复的地方。我没有验证它是否不反对用双引号括起来的表达式的形式,但这是一个合理的假设,它是可以的。

#2


106  

Try this:

试试这个:

[ -z "$STATE" ] && echo "Need to set STATE" && exit 1;

#3


52  

Your question is dependent on the shell that you are using.

你的问题取决于你用的壳层。

Bourne shell leaves very little in the way of what you're after.

伯恩·谢尔几乎没有留下任何你想要的东西。

BUT...

但是…

It does work, just about everywhere.

它确实管用,几乎在任何地方都管用。

Just try and stay away from csh. It was good for the bells and whistles it added, compared the Bourne shell, but it is really creaking now. If you don't believe me, just try and separate out STDERR in csh! (-:

试着远离csh。这对它的铃声和口哨声很有好处,相比之下伯恩的外壳,但它现在真的吱吱作响。如果你不相信我,那就试着用csh把STDERR区分开!(-):

There are two possibilities here. The example above, namely using:

这里有两种可能性。上面的例子,即使用:

${MyVariable:=SomeDefault}

for the first time you need to refer to $MyVariable. This takes the env. var MyVariable and, if it is currently not set, assigns the value of SomeDefault to the variable for later use.

第一次需要引用$MyVariable。这需要env。var MyVariable,如果当前未设置,则将SomeDefault的值赋给该变量,以供以后使用。

You also have the possibility of:

你也有可能:

${MyVariable:-SomeDefault}

which just substitutes SomeDefault for the variable where you are using this construct. It doesn't assign the value SomeDefault to the variable, and the value of MyVariable will still be null after this statement is encountered.

它只是用某个默认值替换你使用这个结构的变量。它不会为变量赋值SomeDefault,在遇到此语句之后MyVariable的值仍然为null。

#4


43  

Surely the simplest approach is to add the -u switch to the shebang (the line at the top of your script), assuming you’re using bash:

当然,最简单的方法是将-u开关添加到shebang(脚本顶部的一行)中,假设您正在使用bash:

#!/bin/sh -u

# !/bin/sh - u

This will cause the script to exit if any unbound variables lurk within.

如果有任何未绑定的变量潜伏在内部,这将导致脚本退出。

#5


30  

${MyVariable:=SomeDefault}

If MyVariable is set and not null, it will reset the variable value (= nothing happens).
Else, MyVariable is set to SomeDefault.

如果设置了MyVariable而不是null,它将重置该变量值(=什么都没有发生)。否则,MyVariable将被设置为默认值。

The above will attempt to execute ${MyVariable}, so if you just want to set the variable do:

上面将尝试执行${MyVariable},因此如果您只想设置变量do:

MyVariable=${MyVariable:=SomeDefault}

#6


18  

In my opinion the simplest and most compatible check for #!/bin/sh is:

在我看来,#是最简单和最兼容的检查!/bin/sh是:

if [ "$MYVAR" = "" ]then   echo "Does not exist"else   echo "Exists"fi

Again, this is for /bin/sh and is compatible also on old Solaris systems.

同样,这是针对/bin/sh的,并且在旧的Solaris系统上也是兼容的。

#7


13  

I always used:

我总是使用:

if [ "x$STATE" == "x" ]; then echo "Need to set State"; exit 1; fi

Not that much more concise, I'm afraid.

恐怕没有那么简洁。

Under CSH you have $?STATE.

在CSH下你有$?STATE。

#8


10  

bash 4.2 introduced the -v operator which tests if a name is set to any value, even the empty string.

bash 4.2引入了-v操作符,它测试名称是否被设置为任何值,甚至是空字符串。

$ unset a$ b=$ c=$ [[ -v a ]] && echo "a is set"$ [[ -v b ]] && echo "b is set"b is set$ [[ -v c ]] && echo "c is set"c is set

#9


1  

None of the above solutions worked for my purposes, in part because I checking the environment for an open-ended list of variables that need to be set before starting a lengthy process. I ended up with this:

以上的解决方案对我的目的都不起作用,部分原因是我在开始一个冗长的过程之前检查环境中需要设置的开放式变量列表。我的结论是:

mapfile -t arr < variables.txtEXITCODE=0for i in "${arr[@]}"do   ISSET=$(env | grep ^${i}= | wc -l)   if [ "${ISSET}" = "0" ];   then      EXITCODE=-1      echo "ENV variable $i is required."   fidoneexit ${EXITCODE}

#10


0  

We can write a nice assertion to check a bunch of variables all at once:

我们可以写一个很好的断言来检查一堆变量:

## assert if variables are set (to a non-empty string)# if any variable is not set, exit 1 (when -f option is set) or return 1 otherwise## Usage: assert_not_null [-f] variable ...#function assert_not_null() {  local fatal var num_null=0  [[ "$1" = "-f" ]] && { shift; fatal=1; }  for var in "$@"; do    [[ -z "${!var}" ]] &&      printf '%s\n' "Variable '$var' not set" >&2 &&      ((num_null++))  done  if ((num_null > 0)); then    [[ "$fatal" ]] && exit 1    return 1  fi  return 0}

Sample invocation:

示例调用:

one=1 two=2assert_not_null one twoecho test 1: return_code=$?assert_not_null one two threeecho test 2: return_code=$?assert_not_null -f one two threeecho test 3: return_code=$? # this code shouldn't execute

Output:

输出:

test 1: return_code=0Variable 'three' not settest 2: return_code=1Variable 'three' not set

#11


0  

For future people like me, I wanted to go a step forward and parameterize the var name, so I can loop over a variable sized list of variable names:

对于像我这样的未来的人,我想要向前一步,对var名称进行参数化,这样我就可以对变量名称的变量列表进行循环:

#!/bin/bashdeclare -a vars=(NAME GITLAB_URL GITLAB_TOKEN)for var_name in "${vars[@]}"do  if [ -z "$(eval "echo \$$var_name")" ]; then    echo "Missing environment variable $var_name"    exit 1  fidone

#12


-1  

This can be a way too:

这也是一种方式:

if (set -u; : $HOME) 2> /dev/null......

http://unstableme.blogspot.com/2007/02/checks-whether-envvar-is-set-or-not.html

http://unstableme.blogspot.com/2007/02/checks-whether-envvar-is-set-or-not.html

#13


-1  

Rather than using external shell scripts I tend to load in functions in my login shell. I use something like this as a helper function to check for environment variables rather than any set variable:

我倾向于在登录shell中加载函数,而不是使用外部shell脚本。我使用类似这样的辅助函数来检查环境变量,而不是任何集合变量:

is_this_an_env_variable ()    local var="$1"    if env |grep -q "^$var"; then       return 0    else       return 1    fi }

#14


-7  

The $? syntax is pretty neat:

美元吗?语法非常简洁:

if [ $?BLAH == 1 ]; then     echo "Exists"; else     echo "Does not exist"; fi

#1


500  

Parameter Expansion

The obvious answer is to use one of the special forms of parameter expansion:

最明显的答案是使用一种特殊形式的参数扩展:

: ${STATE?"Need to set STATE"}: ${DEST:?"Need to set DEST non-empty"}

Or, better (see section on 'Position of double quotes' below):

或者更好(见下面“双引号位置”一节):

: "${STATE?Need to set STATE}": "${DEST:?Need to set DEST non-empty}"

The first variant (using just ?) requires STATE to be set, but STATE="" (an empty string) is OK — not exactly what you want, but the alternative and older notation.

第一个变量(使用just ?)需要设置状态,但是STATE=""(空字符串)是可以的——不是您想要的,而是可选的和旧的符号。

The second variant (using :?) requires DEST to be set and non-empty.

第二个变体(使用:?)需要将DEST设置为非空。

If you supply no message, the shell provides a default message.

如果不提供消息,shell将提供一个默认消息。

The ${var?} construct is portable back to Version 7 UNIX and the Bourne Shell (1978 or thereabouts). The ${var:?} construct is slightly more recent: I think it was in System III UNIX circa 1981, but it may have been in PWB UNIX before that. It is therefore in the Korn Shell, and in the POSIX shells, including specifically Bash.

$ { var吗?}构造可以移植到第7版UNIX和Bourne Shell(1978)。$ { var:?}构造稍微更近一些:我认为它在1981年前后的System III UNIX中出现过,但在那之前它可能在PWB UNIX中出现过。因此,它在Korn Shell中,以及POSIX Shell中,包括特定的Bash。

It is usually documented in the shell's man page in a section called Parameter Expansion. For example, the bash manual says:

通常在shell的手册页中有一个名为参数展开的部分。例如,bash手册说:

${parameter:?word}

Display Error if Null or Unset. If parameter is null or unset, the expansion of word (or a message to that effect if word is not present) is written to the standard error and the shell, if it is not interactive, exits. Otherwise, the value of parameter is substituted.

如果为空或未设置,则显示错误。如果参数为空或未设置,则将word的扩展(或者如果没有word,则将其扩展为相应的消息)写入标准错误,如果非交互式,则将退出shell。否则,将替换参数的值。

The Colon Command

I should probably add that the colon command simply has its arguments evaluated and then succeeds. It is the original shell comment notation (before '#' to end of line). For a long time, Bourne shell scripts had a colon as the first character. The C Shell would read a script and use the first character to determine whether it was for the C Shell (a '#' hash) or the Bourne shell (a ':' colon). Then the kernel got in on the act and added support for '#!/path/to/program' and the Bourne shell got '#' comments, and the colon convention went by the wayside. But if you come across a script that starts with a colon, now you will know why.

我应该补充一句,冒号命令只需要对其参数进行评估,然后成功。它是最初的shell注释符号(在“#”到行尾之前)。很长一段时间,伯恩的shell脚本都有一个冒号作为第一个字符。C Shell将读取脚本并使用第一个字符来确定它是用于C Shell('#'哈希)还是Bourne Shell (a ':'冒号)。然后内核加入进来,添加了对“#!”的支持。/path/to/program'和Bourne shell得到了'#'注释,冒号约定被抛弃。但是如果您遇到一个以冒号开头的脚本,现在您就知道为什么了。


Position of double quotes

blong asked in a comment:

布隆评论道:

Any thoughts on this discussion? https://github.com/koalaman/shellcheck/issues/380#issuecomment-145872749

对这次讨论有什么想法吗?https://github.com/koalaman/shellcheck/issues/380 # issuecomment - 145872749

The gist of the discussion is:

讨论的要点是:

… However, when I shellcheck it (with version 0.4.1), I get this message:

但是,当我对它进行shell检查时(使用0.4.1版本),我得到如下信息:

In script.sh line 13:: ${FOO:?"The environment variable 'FOO' must be set and non-empty"}  ^-- SC2086: Double quote to prevent globbing and word splitting.

Any advice on what I should do in this case?

在这种情况下我该怎么做?

The short answer is "do as shellcheck suggests":

简短的回答是“照shellcheck建议做”:

: "${STATE?Need to set STATE}": "${DEST:?Need to set DEST non-empty}"

To illustrate why, study the following. Note that the : command doesn't echo its arguments (but the shell does evaluate the arguments). We want to see the arguments, so the code below uses printf "%s\n" in place of :.

为了说明原因,请学习以下内容。注意:命令不回显它的参数(但是shell会计算参数)。我们希望看到参数,因此下面的代码使用printf“%s\n”代替:。

$ mkdir junk$ cd junk$ > abc$ > def$ > ghi$ $ x="*"$ printf "%s\n" ${x:?You must set x}    # Careless; not recommendedabcdefghi$ unset x$ printf "%s\n" ${x:?You must set x}    # Careless; not recommendedbash: x: You must set x$ printf "%s\n" "${x:?You must set x}"  # Careful: should be usedbash: x: You must set x$ x="*"$ printf "%s\n" "${x:?You must set x}"  # Careful: should be used*$ printf "%s\n" ${x:?"You must set x"}  # Not quite careful enoughabcdefghi$ x=$ printf "%s\n" ${x:?"You must set x"}  # Not quite careful enoughbash: x: You must set x$ unset x$ printf "%s\n" ${x:?"You must set x"}  # Not quite careful enoughbash: x: You must set x$ 

Note how the value in $x is expanded to first * and then a list of file names when the overall expression is not in double quotes. This is what shellcheck is recommending should be fixed. I have not verified that it doesn't object to the form where the expression is enclosed in double quotes, but it is a reasonable assumption that it would be OK.

注意,当整个表达式不是双引号时,$x中的值如何扩展为first *,然后是文件名列表。这就是shellcheck建议要修复的地方。我没有验证它是否不反对用双引号括起来的表达式的形式,但这是一个合理的假设,它是可以的。

#2


106  

Try this:

试试这个:

[ -z "$STATE" ] && echo "Need to set STATE" && exit 1;

#3


52  

Your question is dependent on the shell that you are using.

你的问题取决于你用的壳层。

Bourne shell leaves very little in the way of what you're after.

伯恩·谢尔几乎没有留下任何你想要的东西。

BUT...

但是…

It does work, just about everywhere.

它确实管用,几乎在任何地方都管用。

Just try and stay away from csh. It was good for the bells and whistles it added, compared the Bourne shell, but it is really creaking now. If you don't believe me, just try and separate out STDERR in csh! (-:

试着远离csh。这对它的铃声和口哨声很有好处,相比之下伯恩的外壳,但它现在真的吱吱作响。如果你不相信我,那就试着用csh把STDERR区分开!(-):

There are two possibilities here. The example above, namely using:

这里有两种可能性。上面的例子,即使用:

${MyVariable:=SomeDefault}

for the first time you need to refer to $MyVariable. This takes the env. var MyVariable and, if it is currently not set, assigns the value of SomeDefault to the variable for later use.

第一次需要引用$MyVariable。这需要env。var MyVariable,如果当前未设置,则将SomeDefault的值赋给该变量,以供以后使用。

You also have the possibility of:

你也有可能:

${MyVariable:-SomeDefault}

which just substitutes SomeDefault for the variable where you are using this construct. It doesn't assign the value SomeDefault to the variable, and the value of MyVariable will still be null after this statement is encountered.

它只是用某个默认值替换你使用这个结构的变量。它不会为变量赋值SomeDefault,在遇到此语句之后MyVariable的值仍然为null。

#4


43  

Surely the simplest approach is to add the -u switch to the shebang (the line at the top of your script), assuming you’re using bash:

当然,最简单的方法是将-u开关添加到shebang(脚本顶部的一行)中,假设您正在使用bash:

#!/bin/sh -u

# !/bin/sh - u

This will cause the script to exit if any unbound variables lurk within.

如果有任何未绑定的变量潜伏在内部,这将导致脚本退出。

#5


30  

${MyVariable:=SomeDefault}

If MyVariable is set and not null, it will reset the variable value (= nothing happens).
Else, MyVariable is set to SomeDefault.

如果设置了MyVariable而不是null,它将重置该变量值(=什么都没有发生)。否则,MyVariable将被设置为默认值。

The above will attempt to execute ${MyVariable}, so if you just want to set the variable do:

上面将尝试执行${MyVariable},因此如果您只想设置变量do:

MyVariable=${MyVariable:=SomeDefault}

#6


18  

In my opinion the simplest and most compatible check for #!/bin/sh is:

在我看来,#是最简单和最兼容的检查!/bin/sh是:

if [ "$MYVAR" = "" ]then   echo "Does not exist"else   echo "Exists"fi

Again, this is for /bin/sh and is compatible also on old Solaris systems.

同样,这是针对/bin/sh的,并且在旧的Solaris系统上也是兼容的。

#7


13  

I always used:

我总是使用:

if [ "x$STATE" == "x" ]; then echo "Need to set State"; exit 1; fi

Not that much more concise, I'm afraid.

恐怕没有那么简洁。

Under CSH you have $?STATE.

在CSH下你有$?STATE。

#8


10  

bash 4.2 introduced the -v operator which tests if a name is set to any value, even the empty string.

bash 4.2引入了-v操作符,它测试名称是否被设置为任何值,甚至是空字符串。

$ unset a$ b=$ c=$ [[ -v a ]] && echo "a is set"$ [[ -v b ]] && echo "b is set"b is set$ [[ -v c ]] && echo "c is set"c is set

#9


1  

None of the above solutions worked for my purposes, in part because I checking the environment for an open-ended list of variables that need to be set before starting a lengthy process. I ended up with this:

以上的解决方案对我的目的都不起作用,部分原因是我在开始一个冗长的过程之前检查环境中需要设置的开放式变量列表。我的结论是:

mapfile -t arr < variables.txtEXITCODE=0for i in "${arr[@]}"do   ISSET=$(env | grep ^${i}= | wc -l)   if [ "${ISSET}" = "0" ];   then      EXITCODE=-1      echo "ENV variable $i is required."   fidoneexit ${EXITCODE}

#10


0  

We can write a nice assertion to check a bunch of variables all at once:

我们可以写一个很好的断言来检查一堆变量:

## assert if variables are set (to a non-empty string)# if any variable is not set, exit 1 (when -f option is set) or return 1 otherwise## Usage: assert_not_null [-f] variable ...#function assert_not_null() {  local fatal var num_null=0  [[ "$1" = "-f" ]] && { shift; fatal=1; }  for var in "$@"; do    [[ -z "${!var}" ]] &&      printf '%s\n' "Variable '$var' not set" >&2 &&      ((num_null++))  done  if ((num_null > 0)); then    [[ "$fatal" ]] && exit 1    return 1  fi  return 0}

Sample invocation:

示例调用:

one=1 two=2assert_not_null one twoecho test 1: return_code=$?assert_not_null one two threeecho test 2: return_code=$?assert_not_null -f one two threeecho test 3: return_code=$? # this code shouldn't execute

Output:

输出:

test 1: return_code=0Variable 'three' not settest 2: return_code=1Variable 'three' not set

#11


0  

For future people like me, I wanted to go a step forward and parameterize the var name, so I can loop over a variable sized list of variable names:

对于像我这样的未来的人,我想要向前一步,对var名称进行参数化,这样我就可以对变量名称的变量列表进行循环:

#!/bin/bashdeclare -a vars=(NAME GITLAB_URL GITLAB_TOKEN)for var_name in "${vars[@]}"do  if [ -z "$(eval "echo \$$var_name")" ]; then    echo "Missing environment variable $var_name"    exit 1  fidone

#12


-1  

This can be a way too:

这也是一种方式:

if (set -u; : $HOME) 2> /dev/null......

http://unstableme.blogspot.com/2007/02/checks-whether-envvar-is-set-or-not.html

http://unstableme.blogspot.com/2007/02/checks-whether-envvar-is-set-or-not.html

#13


-1  

Rather than using external shell scripts I tend to load in functions in my login shell. I use something like this as a helper function to check for environment variables rather than any set variable:

我倾向于在登录shell中加载函数,而不是使用外部shell脚本。我使用类似这样的辅助函数来检查环境变量,而不是任何集合变量:

is_this_an_env_variable ()    local var="$1"    if env |grep -q "^$var"; then       return 0    else       return 1    fi }

#14


-7  

The $? syntax is pretty neat:

美元吗?语法非常简洁:

if [ $?BLAH == 1 ]; then     echo "Exists"; else     echo "Does not exist"; fi