427

我正在查看以下代码:

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

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

4

4 回答 4

565

-z string 如果字符串为空(空字符串)则为真

于 2013-08-07T07:04:01.327 回答
65
-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.
于 2013-08-07T07:03:24.893 回答
31

test -z如果参数为空,则返回 true(参见man shman test)。

于 2013-08-07T07:02:40.317 回答
21

如果字符串的长度为 ,则表达式-z string为真zero

于 2013-08-07T07:02:18.997 回答