Re: Inconsistent arithmetic evaluation of parameters
From:
Chet Ramey
Subject:
Re: Inconsistent arithmetic evaluation of parameters
Date:
Wed, 2 Sep 2015 10:18:25 -0400
User-agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:38.0) Gecko/20100101 Thunderbird/38.2.0
On 9/1/15 4:13 PM, Dennis Williamson wrote:
> The version of dash I have handy (0.5.7) has math support which IMHO is
> broken:
>
> $ foo=bar
> $ bar=5
> $ echo $foo
> bar
> $ echo $((foo))
> dash: 4: Illegal number: bar
> $ echo $(($foo))
> 5
> $ echo $((bar))
> 5
> $ echo $(($bar))
> 5
Dash does the minimum that Posix requires for $(( )) and no more.
The stuff between $(( and )) is expanded and the result is treated as an
arithmetic expression. When there is something that looks like an
identifier (e.g., `foo') remaining after expansion, it's treated as a
shell variable whose value is assumed to be an integer constant, not an
expression. Since foo expands to `bar', and `bar' is not an integer, dash
throws an error.
>
> Note the inconsistency in support of omitting the inner dollar sign. Also
> post increment produces an error and pre increment produces no error and no
> action:
Posix allows implementations to omit support for prefix and postfix ++ and
--. If you don't implement prefix ++ and --, they are unary plus and
minus operators that happen to `stack'.
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU chet@case.edu http://cnswww.cns.cwru.edu/~chet/