-z在Bash中意味着什么?

Noi*_*ich 253 bash

我正在看下面的代码:

if [ -z $2 ]; then
        echo "usage: ...
Run Code Online (Sandbox Code Playgroud)

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

Yu *_*Hao 339

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

  • 另外,`-n`与`-z`相反.`if [-n"$ {1}"]`如果字符串不为空且不为空则传递. (34认同)
  • -n是否具有默认行为if [$ [2]]还是有一些区别? (4认同)
  • 我找到了另一个出色且详细的解释 - http://stackoverflow.com/questions/3601515/how-to-check-if-a-variable-is-set-in-bash (2认同)

小智 48

-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.
Run Code Online (Sandbox Code Playgroud)


kni*_*ttl 17

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


moh*_*hit 10

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

  • 如果它是未定义的字符串(如果这是一个东西)或者是一个数字怎么办? (3认同)