Keith Thompson wrote:
> I just tried it myself in bash 4.3.11:
>
> $ if [[ "" -lt 5 ]] ; then echo yes ; else echo no ; fi
> yes
> $ if [ "" -lt 5 ] ; then echo yes ; else echo no ; fi
> bash: [: : integer expression expected
> no
> $
>
> I'm not overly surprised that [ and [[ behave differently, but I see no
> particular reason for [ to print an error message while [[ doesn't.
ISTM to be obvious: “[[ … ]]” does type conversion; “[ … ]”, equivalent to
“test”, does not. This probably has to do with backwards compatibility; “[[
… ]]” is a *non-standard* feature that is a *compound* command –
| Compound Commands
|
| […]
|
| [[ expression ]]
| Return a status of 0 or 1 depending on the evaluation of the
| conditional expression expression. Expressions are
| composed of the primaries described below under
| CONDITIONAL EXPRESSIONS. Word splitting and pathname
| expansion are not performed on the words between the [[ and
| ]]; tilde expansion, parameter and variable expansion,
| arithmetic expansion, command substitution, process sub‐
| stitution, and quote removal are performed. Conditional
| operators such as -f must be unquoted to be recognized as
| primaries.
|
| When used with [[, the < and > operators sort
| lexicographically using the current locale.
– while “[”, respectively “test”, is a *standard* feature that is a *simple*
built-in command:
| SHELL BUILTIN COMMANDS
|
| […]
|
| test expr
| [ expr ]
| Return a status of 0 (true) or 1 (false) depending on the
| evaluation of the conditional expression expr. Each
| operator and operand must be a separate argument.
| Expressions are composed of the primaries described above
| under CONDITIONAL EXPRESSIONS. test does
| not accept any options, nor does it accept and ignore an
| argument of -- as signifying the end of options.
|
| test and [ evaluate conditional expressions using a set of
| rules based on the number of arguments.
|
| […]
| 3 arguments
| The following conditions are applied in the order
| listed. If the second argument is one of the binary
| conditional operators listed above under
| CONDITIONAL EXPRESSIONS, the result of the expression
| is the result of the binary test using the first
| and third arguments as operands. The -a and -o
| operators are considered binary operators when there
| are three arguments.
| If the first argument is !, the value is the negation
| of the two-argument test using the second and third
| arguments. If the first argument is exactly ( and
| the third argument is exactly ), the result is the
| one-argument test of the second argument.
| Otherwise, the expression is false.
`----
See also:
<
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html>
> The bash documentation is not illuminating; the section on [[ doesn't
> specifically mention numeric comparison.
True.
> […] IMHO
>
> [[ "" -lt 5 ]]
>
> *should* produce an error message, though the documentation doesn't
> explicitly say so. The fact that it doesn't is probably a bug.
It is a *documentation* bug that it the documentation does not say that no
error message is produced, and that type conversion is performed instead.