On Thu, Sep 3, 2015 at 8:45 AM, Stephane Chazelas < stephane.chaze...@gmail.com> wrote:
> 2015-09-01 16:23:08 -0400, Greg Wooledge: > > On Tue, Sep 01, 2015 at 03:13:57PM -0500, 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 > > > > > > Note the inconsistency in support of omitting the inner dollar sign. > > > > $foo is expanded to bar, so the following two lines are always going to > > be equivalent: > > > > echo $(($foo)) > > echo $((bar)) > > > > POSIX also specifies (vaguely!!) that $((x)) and $(($x)) are equivalent. > > Note that while POSIX may (vaguely indeed) say $((x)) and > $(($x)) are equivalent at least when x contains a litteral > number, $((-x)) and $((-$x)) are not equivalent in all shells if > $x contains a negative number. > > $ a=0 x=-1 bash -c 'echo $((a-$x))' > bash: a--1: syntax error in expression (error token is "1") > $ a=0 x=-1 bash -c 'echo $((a-x))' > 1 > > (they're OK in shells that don't implement the (optional in > posix) -- and ++ operators like dash. > > Or you can add spaces to make it more reliable: $((a - $x)) > > In ksh/zsh/bash, see also the difference between: > > $ a=1+1; echo $((a*2)) > 4 > $ a=1+1; echo $(($a*2)) > 3 > > -- > Stephane > > > > > > > Also, for completeness: $ a=0 x=-1 bash -c 'echo $(($a-$x))' 1 -- Visit serverfault.com to get your system administration questions answered.