两者之间的确切区别是什么:
if [ $? -ne 0 ];
和
if [[ $? -ne 0 ]];
如此处所述:
相反
[
,[[
防止变量值的分词。因此,如果VAR="var with spaces"
,您不需要$VAR
在测试中使用双引号 - 即使使用引号仍然是一个好习惯。此外,[[
防止路径名扩展,因此带有通配符的文字字符串不会尝试扩展为文件名。使用[[
,==
并将!=
右侧的字符串解释为要与左侧的值匹配的 shell glob 模式,例如:[[ "value" == val* ]]
.
空无一人。不过,[[
...]]
语法介绍了您可以使用条件表达式执行的其他一些操作。来自help [[
:
Returns a status of 0 or 1 depending on the evaluation of the conditional
expression EXPRESSION. Expressions are composed of the same primaries used
by the `test' builtin, and may be combined using the following operators:
( EXPRESSION ) Returns the value of EXPRESSION
! EXPRESSION True if EXPRESSION is false; else false
EXPR1 && EXPR2 True if both EXPR1 and EXPR2 are true; else false
EXPR1 || EXPR2 True if either EXPR1 or EXPR2 is true; else false
When the `==' and `!=' operators are used, the string to the right of
the operator is used as a pattern and pattern matching is performed.
When the `=~' operator is used, the string to the right of the operator
is matched as a regular expression.