2

What's the precise difference between:

if [ $? -ne 0 ];

and

if [[ $? -ne 0 ]];
Michael Hoffman
  • 32,526
  • 7
  • 64
  • 86
contrapositive
  • 1,381
  • 2
  • 10
  • 11

2 Answers2

3

As stated here:

Contrary to [, [[ prevents word splitting of variable values. So, if VAR="var with spaces", you do not need to double quote $VAR in a test - eventhough using quotes remains a good habit. Also, [[ prevents pathname expansion, so literal strings with wildcards do not try to expand to filenames. Using [[, == and != interpret strings to the right as shell glob patterns to be matched against the value to the left, for instance: [[ "value" == val* ]].

vergenzt
  • 9,669
  • 4
  • 40
  • 47
higuaro
  • 15,730
  • 4
  • 36
  • 43
  • 1
    A small addendum: The abilities like prevention of word splitting and path name expansion are a consequence of the fact that `[[` is a built-in as compared to `[` which is an external command. – Samveen Jun 29 '12 at 06:50
  • 1
    @Samveen: `[` is an external command. But it's also a builtin. – Dennis Williamson Jun 29 '12 at 08:50
2

There is none. The [[...]] syntax introduces some other things you can do with conditional expressions, though. From 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.
Michael Hoffman
  • 32,526
  • 7
  • 64
  • 86