help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] Arithmetic evaluation / expansion question


From: Chet Ramey
Subject: Re: [Help-bash] Arithmetic evaluation / expansion question
Date: Thu, 11 Oct 2018 10:33:55 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:52.0) Gecko/20100101 Thunderbird/52.9.1

On 10/10/18 1:07 PM, Greg Wooledge wrote:
> On Wed, Oct 10, 2018 at 12:47:05PM -0400, Bruce Hohl wrote:
>> Are strings also evaluated to zero within $(( ))?
>>
>> $ pd=abc
>> $ echo $pd
>> abc
>> $ echo $((pd))
>> 0
> 
> As Andy said, things that can be parsed as variable names are treated
> as variable names in arithmetic contexts.  What he didn't mention was
> that bash does this *recursively* until it gets to an integer, or to
> something that can't be parsed as either an integer or a variable name.

No, Greg. If the evaluator encounters a token that looks like a shell
identifier, its value is treated as an expression (if it's unset, that
expression evaluates to 0). This expression is run through the evaluator
again, as if it had been seen inline in parens. This is what makes

ten='2 * 5'
echo $(( ten ))
echo $(( $ten ))

both echo `10'. I'm surprised you said that, because this has come up a
number of times in the past, but your wiki has the same error.

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



reply via email to

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