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: Peggy Russell
Subject: Re: [Help-bash] Conditional expression evaluation using [ and [[ with -eq operator
Date: Sat, 16 Nov 2013 20:43:36 -0600
User-agent: KMail/1.12.4 (Linux/2.6.31.12-0.2-desktop; KDE/4.3.5; x86_64; ; )

> > 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.

If [ is a shell builtin, and [[ a shell keyword, asis {, what is (( to bash?
I know $(( is used in arithmetic substitution and (( arithmetic evaluation. 

type test [ [[ { let \(\(
 test is a shell builtin
 [ is a shell builtin
 [[ is a shell keyword
 { is a shell keyword
 let is a shell builtin
 bash: type: ((: not found

I've read forums, where folks will say [[ is enhanced [ or $(($a+1)) is the 
same as $((a+1)), and on the surface the user may perceive that, but it 
could cause problems down the line. 

All responses were helpful.

Thank you.
Peggy Russell



reply via email to

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