我正在看下面的代码:

if [ -z $2 ]; then
        echo "usage: ...

(3个点是不相关的使用细节。) 也许我搜索错了,但我找不到-z选项的解释。


当前回答

-z

string is null, that is, has zero length

String=''   # Zero-length ("null") string variable.

if [ -z "$String" ]
then
  echo "\$String is null."
else
  echo "\$String is NOT null."
fi     # $String is null.

其他回答

-z string:如果字符串为null(空字符串)则为True

看到https://www.gnu.org/software/bash/manual/bash.html Bash-Conditional-Expressions

-z

string is null, that is, has zero length

String=''   # Zero-length ("null") string variable.

if [ -z "$String" ]
then
  echo "\$String is null."
else
  echo "\$String is NOT null."
fi     # $String is null.

Test -z如果参数为空则返回true(参见man sh或man Test)。

如果string的长度为0,则表达式-z string为真。