bug-bash
[Top][All Lists]
Advanced

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

Re: Curious case of arithmetic expansion


From: Florian Mayer
Subject: Re: Curious case of arithmetic expansion
Date: Sun, 23 Apr 2017 15:50:13 +0200

what shoud echo $((foo++)) do?
Give out an error message hopefully (and it does), because 
the _expression_ (bar+14+moo*2)++ makes no semantical sense
in terms of arithmetic expressions the bash uses, because ++
only can apply to locations and (bar+14+moo*2) evaluates to a number no matter
the contents of bar and moo…

So lets assume that bash only executes things like ((<exp>++)) if and only if
<exp> is a proper location (that is a string that is no number).
We know that in this case only two things can happen.
(1) <exp> refers directly to a number value
or (2) <exp> refers to another string that is a location

In case of (1) the result is obvious. In case of (2) my complain applies.

Your case is only really a special case, arguably having only indirection instead of what we have 
would perhaps have been a better idea, but it is this way for so long that I doubt it will change.
I wouldn’t consider this a special case, because the syntax and semantics
are almost identical to those of C (with the exception of the numerous exceptions like
recursive expansions etc.). So bash should also implement the same lvalue, rvalue
semantics that C does. Of course, with the appropriate adjustments needed to fit in the
context of a string typed command line interpreter :)

would perhaps have been a better idea, but it is this way for so long that I doubt it will change.
Mhh…

PS: you can perhaps use name reference instead 
moo=1;
declare -n foo=bar bar=moo
echo $((foo++))
echo $moo
Thank you.

Am 23.04.2017 um 15:19 schrieb Pierre Gaston <address@hidden>:



On Sun, Apr 23, 2017 at 4:07 PM, Florian Mayer <address@hidden> wrote:
It does not matter, how this construct in this particular context is called.
The difference between $(()) and (()) is that $(()) actually expands to something
whereas (()) just executes a C-like _expression_. In ((<_expression_>)) <_expression_> can also
include assignments, as the bash manual that you properly cited, also elaborates on.
You can do, for example, things like
$ foo=2
$ ((foo+=100)) # fo is now 102
$ ((++(foo++)))
or even
$ ((foo++, foo++, foo++, foo++, foo+=100))
and (oh boy why) even
$ foo=(123 321)
$ ((foo[0]++, foo[1]—))

So I might have chosen the wrong subject text for this mail,
but again, it does not matter whether those constructs actually expand to some string
or not. The side effects are what matter here. And in my opinion those are not correct...

I understand what you want, I'm just explaining the result you get
and why it doesn't do what you want.
As it is, a variable can contain the name of another variable
but it can also contain any arbitrary string that is a correct arithmetic _expression_.

So if you have:

foo=bar+14+baz
baz='moo*2'
moo=1

what shoud echo $((foo++)) do?

Your case is only really a special case, arguably having only indirection instead of what we have
would perhaps have been a better idea, but it is this way for so long that I doubt it will change.

PS: you can perhaps use name reference instead
moo=1;
declare -n foo=bar bar=moo
echo $((foo++))
echo $moo




reply via email to

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