繁体   English   中英

使用[和[[

[英]Difference between conditional expressions using [ and [[

之间的确切区别是什么?

if [ $? -ne 0 ];

if [[ $? -ne 0 ]];

如前所述这里

[[[相反,可防止变量值的单词分割。 因此,如果VAR="var with spaces" ,则无需在测试中双引号$VAR VAR-尽管使用引号仍然是一个好习惯。 另外, [[防止路径名扩展,因此带有通配符的文字字符串不会尝试扩展为文件名。 使用[[==!=将右边的字符串解释为要与左边的值匹配的shell球形模式,例如: [[ "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.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM