[Top][All Lists]

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

Re: Curious case of arithmetic expansion

From: Pierre Gaston
Subject: Re: Curious case of arithmetic expansion
Date: Sun, 23 Apr 2017 16:19:52 +0300

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:


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
declare -n foo=bar bar=moo
echo $((foo++))
echo $moo

reply via email to

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