Jerry Peters wrote:
> Thomas 'PointedEars' Lahn <
Point...@web.de> wrote:
>> Ed Morton wrote:
>>> Tom <
zi...@yahoo.com> wrote:
>>>> I have the following bash code:
>>>>
>>>> #!/bin/bash
>>>> x=0
>>>> /bin/cat >bar <<End-of-message
>>>> This is a test message.
>>>> End-of-message
>>>>
>>>> if [ $x == 0 ]; then
>>>
>>> The shell test for equality is "=" not "==".
>>
>> That is not entirely correct. The OP is using a certain shell, bash,
>> where `==' is an equality operator with special features:
>>
>> | When the == and != operators are used, the string to the right
>> | of the operator is considered a pattern and matched according to
>> | the rules described below under Pattern Matching. If the shell
>> | option nocase?match is enabled, the match is performed without
>> | regard to
>> | the case of alphabetic characters. The return value is 0 if the
>> | string matches (==) or does not match (!=) the pattern, and 1
>> | otherwise. Any part of the pattern may be quoted to force it to
>> | be matched as a string.
>
> You omitted the lead-in to that paragraph:
You omitted your e-mail address.
> [[ expression ]]
That is _not_ the lead-in to the paragraph above; it is:
| Compound Commands
However:
$ bash -vxc '[ 1 == 0 ] && echo 42'
[ 1 == 0 ] && echo 42
+ '[' 1 == 0 ']'
$ bash -vxc '[ 1 == 1 ] && echo 42'
[ 1 == 1 ] && echo 42
+ '[' 1 == 1 ']'
+ echo 42
42
$ bash --version
GNU bash, version 4.2.20(1)-release (i486-pc-linux-gnu)
[…]
The reason is:
| CONDITIONAL EXPRESSIONS
| Conditional expressions are used by the [[ compound command and
| the test and [ builtin commands to test file attributes and
| perform string and arithmetic comparisons. Expressions are formed
| from the following unary or binary primaries. […]
|
| […]
| string1 == string2
| string1 = string2
| True if the strings are equal. = should be used with the
| test command for POSIX conformance.
| […]
| SHELL BUILTIN COMMANDS
| […]
|
| test expr
| [ expr ]
|
| Return a status of 0 or 1 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. […]
> When used with [[, The < and > operators sort
> lexicographically using the current locale.
>
> When the == and != operators are used, the string to the
> right of the operator is considered a pattern and matched
> according to the rules described below under Pattern
> Matching.
>
> Note that the OP only used single '[ ]' not '[[ ]]'.
Falsifying quotations meets a supposed anti-social character.