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 22:25:26 +0200

That's not a reasonable expectation.
Why not? Why is it not reasonable to expect an intuitive
result from (())? The most intuitive thing, in my opinion,
would be to use nameref for side effects by default, because in order
to get a value from an id, (()) already follows namerefs.

So if you have another opinion, or don’t want to change that
behaviour, that is totally understandable. But I think that this
surely is a reasonable expectation.

It's not indirection, and I am not sure why you show the completely
I was mentioning that, not because I use „a mental model“ that falsely unifies
both things, but because I wanted to point out that there exists something
with a similar behavior.

And if ${!<varid>} does not portray some kind of indirection, what do you call it then?
I am aware of the ${!<prefix>*} form, what it does and that the following probably
does not internally do what I think it does, but on my system
$ foo=bar; bar=moo; echo ${!foo}
really produces the string moo. Therefore, I referred to it as indirection.

Am 23.04.2017 um 20:02 schrieb Chet Ramey <chet.ramey@case.edu>:

On 4/23/17 8:28 AM, Florian Mayer wrote:
What I’m saying is, that if bash does recursively apply expansion
mechanisms on the identifiers until it can retrieve a number, 
it should do it symmetrically. 

That's not a reasonable expectation.

Here's how it works. When bash reads a token, such as `foo', it reads
it as a string.  In contexts where it needs a variable name, such as
the left side of an assignment, it remains a string. When it needs to
be treated as a value, such as on the right side of an assignment, its
value is expanded as an _expression_ until it results in a number (unset
variables evaluate to 0).

So, foo++ is essentially equivalent to

ofoo=foo, foo=ofoo+1, ofoo

When foo's value is needed, it is evaluated. When foo needs to be assigned
a value, it is assigned a value.  You're arguing for non-determinism when
assigning a value based on whether or not a variable happens to contain a
string. Unless you specifically indicate you want something like that, like
when using namerefs, it's not reasonable.


That is,
it should remember what chain of expansion had been necessary for
a particular number to appear at the end of the expansion.

So instead of 
124 moo 123
The echo command should produce
bar moo 124

(The expansion chain here was foo->bar->moo->123)

It's because it's not really indirection, rather the content of the
variable is evaluated:
No it is really indirection. Bash even has a special (and very limited)
syntax for that.

It's not indirection, and I am not sure why you show the completely
different variable indirection syntax.  The mental model you're using
may equate the two, but they are not the same.

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    chet@case.edu    http://cnswww.cns.cwru.edu/~chet/


reply via email to

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