[freebsd] test и пустые строки

Irina Liakh spell at itl.ua
Tue Nov 21 00:53:33 EET 2017


Приветствую всех!

Всплыло непонятное поведение команды test (builtins из sh и bash и /bin/test),
подскажите, какой в этом смысл?

Из man test:
     string        True if string is not the null string.
     -n string     True if the length of string is nonzero.
     -z string     True if the length of string is zero.

Проверка строки без опций проходит ожидаемо:

$ if [ ]; then echo yes; else echo no; fi
no
$ if [ "" ]; then echo yes; else echo no; fi
no

Но -n, вопреки ожиданиям, ведёт себя не аналогично:

$ if [ -n ]; then echo yes; else echo no; fi
yes
$ if [ -n "" ]; then echo yes; else echo no; fi
no

Первая мысль: -n (а значит и -z тоже) считает, что нулевая длина может быть
только у не null string. Но -z опровергает это:

$ if [ -z ]; then echo yes; else echo no; fi
yes
$ if [ -z "" ]; then echo yes; else echo no; fi
yes


#какжетак?


More information about the freebsd mailing list