On 10/13/14, 10:28 AM, Stephane Chazelas wrote:
> 2014-10-13 09:02:38 -0400, Chet Ramey:
> [...]
>> You have to put it together.  A variable need not be expanded before
>> arithmetic evaluation, so the evaluator expands, for instance, a bare
>> `a' to `b[$(echo 1+1)]'.  When a variable is expanded, its value is
>> treated as an expression to be evaluated.  An array subscript -- for an
>> indexed array -- is an arithmetic expression.  All tokens in an arithmetic
>> expression, including an array subscript, undergo variable expansion, word
>> splitting, and quote removal.
> [...]
> 
> Thanks. I'm still confused though.
> 
> In
> 
> b=2
> a='1 + $b'
> echo $((a))
> 
> a is expanded and the expansion is also evaluated as an
> arithmetic expression, so why isn't $b expanded there?

Because it doesn't look like an identifier.  Subexpressions that
result from the arithmetic expression evaluation code substituting
variable values for names are not run through word expansions.

The idea is that the evaluator has to do something with operands
that are not arithmetic constants.  The ones that meet the requirements
for variable names are treated as variables, and those values get
substituted.  It doesn't mean that the values get run through word
expansion.

> Note that it's not only variable expansion, it's also tilde
> (even though ~ is also an arithmetic operator) expansion.
> 
> $ HOME=1 a='b[~]'  bash -c 'b=(1 2 3); echo $((a))'
> 2
> 
> That means for instance that 
> 
> foo=-1
> echo $((a[~foo]))
> 
> won't work on systems where there's a "foo" user.

OK.


> That makes it a
> difference with other shells.
> 
> $ a='1-+1' c='b[$a]' bash -c 'b=(1 2 3); IFS=-; echo $((c))'
> 3
> $ a='1-+1' c='b[$a]' ksh -c 'b=(1 2 3); IFS=-; echo $((c))'
> 1

That looks like a bug.  I'll take a look.

Chet

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

Reply via email to