bug-bash
[Top][All Lists]
Advanced

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

Re: Inconsistent arithmetic evaluation of parameters


From: Dennis Williamson
Subject: Re: Inconsistent arithmetic evaluation of parameters
Date: Tue, 1 Sep 2015 16:05:15 -0500



On Tue, Sep 1, 2015 at 3:23 PM, Greg Wooledge <wooledg@eeg.ccf.org> wrote:
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))

It's the line above those two where I demonstrate the failure in the indirection and equivalency.

$ echo $((foo))  # expansion succeeds, indirection fails
dash: 4: Illegal number: bar
$ echo $(($foo))  # both expansion and indirection succeed
5
$ echo $((bar))  # expansion succeeds
5
$ echo $(($bar))  # expansion succeeds
5
 
Bash doesn't have this inconsistency. (Just don't omit the dollar sign from $RANDOM in most Bourne-derived shells. https://lists.gnu.org/archive/html/bug-bash/2010-01/msg00039.html)


POSIX also specifies (vaguely!!) that $((x)) and $(($x)) are equivalent.



--
Visit serverfault.com to get your system administration questions answered.

reply via email to

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