Bash手册说:
如果已为参数分配了值,则设置该参数。空字符串是有效值。
...
如果未给出值,则为变量分配空字符串。
空字符串是否与相同""
?
它们的长度都为零吗?可以通过两者的条件表达式来测试-z
或-n
它测试一个字符串的长度是零或非零?
是的。使用来自此答案的测试:
$ [ -n "${a+1}" ] && echo "defined" || echo "not defined"
not defined
$ a=""
$ [ -n "${a+1}" ] && echo "defined" || echo "not defined"
defined
$ b=
$ [ -n "${b+1}" ] && echo "defined" || echo "not defined"
defined
因此,将变量设置""
为与将其设置为空值相同。因此,空值和""
相同。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句