[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Curious case of arithmetic expansion
From: |
Chet Ramey |
Subject: |
Re: Curious case of arithmetic expansion |
Date: |
Sun, 23 Apr 2017 14:02:16 -0400 |
User-agent: |
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:52.0) Gecko/20100101 Thunderbird/52.0.1 |
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/
- Curious case of arithmetic expansion, Florian Mayer, 2017/04/23
- Re: Curious case of arithmetic expansion, Pierre Gaston, 2017/04/23
- Re: Curious case of arithmetic expansion, Florian Mayer, 2017/04/23
- Re: Curious case of arithmetic expansion, Pierre Gaston, 2017/04/23
- Re: Curious case of arithmetic expansion, Florian Mayer, 2017/04/23
- Re: Curious case of arithmetic expansion, Florian Mayer, 2017/04/23
- Re: Curious case of arithmetic expansion, Pierre Gaston, 2017/04/23
- Re: Curious case of arithmetic expansion, Florian Mayer, 2017/04/23
- Re: Curious case of arithmetic expansion,
Chet Ramey <=
- Re: Curious case of arithmetic expansion, Florian Mayer, 2017/04/23
- Re: Curious case of arithmetic expansion, Eduardo Bustamante, 2017/04/23
- Re: Curious case of arithmetic expansion, Chet Ramey, 2017/04/23
- Re: Curious case of arithmetic expansion, Florian Mayer, 2017/04/24
- Re: Curious case of arithmetic expansion, Steve Amerige, 2017/04/24
- Re: Curious case of arithmetic expansion, Chet Ramey, 2017/04/24