help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] Conditional expression evaluation using [ and [[ with -e


From: Chet Ramey
Subject: Re: [Help-bash] Conditional expression evaluation using [ and [[ with -eq operator
Date: Sat, 16 Nov 2013 22:23:15 -0500
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:24.0) Gecko/20100101 Thunderbird/24.0.1

On 11/16/13, 9:43 PM, Peggy Russell wrote:
>>> But in this case, what is it about the command line processing steps for
>>> [ and [[, that the shell builtin test command, [, appears to have determined
>>> this was an integer expression, but the shell keyword compound command, [[,
>>> did not?
>>
> snippet.. 
>> Builtin commands undergo word expansion before being executed.  In this
>> case, that means the words are expanded before being parsed as a
>> conditional expression and evaluated.  A command such as [[, which is part
>> of the shell grammar, does not perform word expansion before the expression
>> is parsed.  That means that the operators in [[ are essentially tokens and
>> must be present literally in order to be recognized by the shell parser.
> 
> snippet...
>> [[ is evaluating the operands as arithmetic expressions when you use -eq
>> -ne -lt etc...  so "a" is taken as the name of a variable and an empty 
>> variable evaluates to 0.
> 
> 'Literally present'... making sense... okay.
> $a goes through expansion first and the whole arithmetic expression is then 
> handled, the "literal a" variable name defaults to 0 when unset/null.

My message was intended as a more complete explanation of how the two
commands differ, but did not include this piece: the test command follows
Posix and requires that the operands to the arithmetic comparison operators
be integers.  The [[ command behaves as Pierre described and evalutes the
operands as arithmetic expressions.

> If [ is a shell builtin, and [[ a shell keyword, asis {, what is (( to bash?

It's a reserved word.  One can intuit this because it introduces a compound
command.  It's probably a bug that`type' doesn't report this; it's due to
how the parser and lexical analyzer determine whether or not (( is an
arithmetic command or a nested subshell.

Chet
-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU    address@hidden    http://cnswww.cns.cwru.edu/~chet/



reply via email to

[Prev in Thread] Current Thread [Next in Thread]