如何知道是否在Bash中设置了变量?
例如,如何检查用户是否为函数提供了第一个参数?
function a {
# if $1 is set ?
}发布于 2012-12-14 01:04:53
(通常)正确的方式
if [ -z ${var+x} ]; then echo "var is unset"; else echo "var is set to '$var'"; fi其中,${var+x}是一个parameter expansion,如果未设置var,则其计算结果为nothing,否则将替换字符串x。
引文离题
引号可以省略(所以我们可以说${var+x}而不是"${var+x}"),因为这种语法和用法保证了它只会扩展到不需要引号的东西(因为它要么扩展到x (它不包含断字符,所以它不需要引号),要么扩展到nothing (这会导致[ -z ],它的计算结果也是[ -z "" ]的相同值(true) )。
然而,虽然可以安全地省略引号,而且不是所有人都能立即清楚地看到这一点(对于同时也是一个主要的Bash程序员的the first author of this quotes explanation来说也不是很明显),但有时使用引号作为[ -z "${var+x}" ]编写解决方案会更好,但可能会以O(1)的速度损失很小的代价。第一作者还在使用此解决方案的代码旁边添加了此注释,给出了此答案的URL,现在还包括了为什么可以安全地省略引号的解释。
(经常)走错了路
if [ -z "$var" ]; then echo "var is blank"; else echo "var is set to '$var'"; fi这通常是错误的,因为它没有区分未设置的变量和设置为空字符串的变量。也就是说,如果是var='',那么上面的解决方案将输出"var is var=''“。
如果用户必须指定扩展或附加属性列表,并且不指定它们的默认值为非空值,而指定空字符串应使脚本使用空扩展或附加属性列表,则unset和"set to the not string“之间的区别是必不可少的。
然而,这种区别可能并不是在每个场景中都是必不可少的。在这种情况下,[ -z "$var" ]就没问题了。
发布于 2010-08-30 23:17:25
若要检查非空/非零字符串变量,即如果设置了该变量,请使用
if [ -n "$1" ]它是-z的对立面。我发现自己使用-n比使用-z更多。
您可以像这样使用它:
if [ -n "$1" ]; then
echo "You supplied the first parameter!"
else
echo "First parameter not supplied."
fi发布于 2013-05-26 04:20:04
下面是如何测试一个参数是未设置的,还是 ("Null")或使用值设置的
+--------------------+----------------------+-----------------+-----------------+
| Expression | parameter | parameter | parameter |
| in script: | Set and Not Null | Set But Null | Unset |
+--------------------+----------------------+-----------------+-----------------+
| ${parameter:-word} | substitute parameter | substitute word | substitute word |
| ${parameter-word} | substitute parameter | substitute null | substitute word |
| ${parameter:=word} | substitute parameter | assign word | assign word |
| ${parameter=word} | substitute parameter | substitute null | assign word |
| ${parameter:?word} | substitute parameter | error, exit | error, exit |
| ${parameter?word} | substitute parameter | substitute null | error, exit |
| ${parameter:+word} | substitute word | substitute null | substitute null |
| ${parameter+word} | substitute word | substitute word | substitute null |
+--------------------+----------------------+-----------------+-----------------+来源:POSIX: Parameter Expansion
替换在使用“
”显示的所有情况下,表达式都将替换为显示的值。在使用"assign“显示的所有情况下,都会为参数分配该值,该值也会替换表达式。
要在实践中展示这一点,请执行以下操作:
+--------------------+----------------------+-----------------+-----------------+
| Expression | FOO="world" | FOO="" | unset FOO |
| in script: | (Set and Not Null) | (Set But Null) | (Unset) |
+--------------------+----------------------+-----------------+-----------------+
| ${FOO:-hello} | world | hello | hello |
| ${FOO-hello} | world | "" | hello |
| ${FOO:=hello} | world | FOO=hello | FOO=hello |
| ${FOO=hello} | world | "" | FOO=hello |
| ${FOO:?hello} | world | error, exit | error, exit |
| ${FOO?hello} | world | "" | error, exit |
| ${FOO:+hello} | hello | "" | "" |
| ${FOO+hello} | hello | hello | "" |
+--------------------+----------------------+-----------------+-----------------+https://stackoverflow.com/questions/3601515
复制相似问题