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: Chet Ramey
Subject: Re: Curious case of arithmetic expansion
Date: Mon, 24 Apr 2017 11:28:26 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:45.0) Gecko/20100101 Thunderbird/45.8.0

On 4/24/17 3:59 AM, Florian Mayer wrote:
> Ok, I accept your points, but please read on and decide after that.
> 
>> to do anything but assign a value to `var'.  Very few people, when asked,
>> would say that it were more intuitive to cause a variable named `bar' to
>> spring into existence with the value 7. If you want nameref behavior, you
>> have to explicitly declare it.
> But why/ /is nameref behavior in the evaluation part activated by default
> and not for the assignments? I think that one should
> either make nameref behavior the default case for both, 
> assignment and evaluation, or the non-default case for both.

OK, you can certainly have whatever opinion you want.  However, 27 years
ago when I put this into bash, there was considerable benefit from
supporting running variable values through the expression evaluator instead
of simply coercing their value to an integer.  That allowed, in the most
basic use case, a simple arithmetic macro facility.  You could also
do more complicated things like compose complex expressions.

There was no similar benefit from recursively expanding variable names.
There still isn't.  And there is no compelling reason for such a radical
break with existing behavior.  Nobody besides you has ever requested it.

> In the latter case, where namerefs are off for both things, the lines (1)
> and (2)
> 1) var=bar; bar=3; echo ((var))
> 2) var=bar; ((var=3))   or var=bar; bar=0; ((var++))
> should lead to an error message!
> However the current status quo is, that bash happily does (1) as you’d 
> expect but gives out no error in (2) and overwrites (1).

You're saying that something like ((var=3)) should produce an error
instead of assign `3' to `var'? Really? Or that var++ should not behave
identically to `ovar=$var, var=$(( ovar + 1 )), ovar'?

-- 
``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]