[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: |
Fri, 15 Nov 2013 14:49:16 -0500 |
User-agent: |
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Thunderbird/24.1.0 |
On 11/15/13 12:41 PM, Peggy Russell wrote:
> I saw the snippet below the other day. Validating data I would use a regular
> expression or pattern in a 'if [[' or 'case', and not the below.
>
> 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?
Builtin commands undergo word expansion before being executed. In this
csae, 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.
It's the difference between `for f in x' and `$for f in x'. Even if the
shell variable `for' had the value `for', would you expect that to work?
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/