help-bash
[Top][All Lists]
Advanced

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

Re: Is it really necessary to allow operators and whole right hand sides


From: Peng Yu
Subject: Re: Is it really necessary to allow operators and whole right hand sides to be substituted in (())?
Date: Mon, 17 May 2021 20:31:10 -0500

On 5/17/21, Chet Ramey <chet.ramey@case.edu> wrote:
> On 5/17/21 5:07 PM, Peng Yu wrote:
>> Hi,
>>
>> $ plus=+; ((x = 1 $plus 2)); declare -p x
>> declare -x x="3"
>>
>> I see that the above code works. I think that allowing operators to be
>> substituted is counterintuitive.
>
> I'm sure you do.
>
> The (( expression )) command is defined to behave this way because that's
> how it was implemented in ksh88. It was defined there as equivalent to
> `let "expression"' (which had been in ksh from the first version, ksh83)
> and `expression' is therefore expanded as if it were within double quotes.
>
> This is consistent with the subsequent definition of $(( expression )),
> where all the tokens in expression are expanded the same way: with
> parameter expansion, command substitution, and quote removal.


> The expression parser gets the results of the expanded expression, just
> like any other command, and parses it accordingly.
>
>
>> So it would be better not to allow it when this syntax first appeared?
>
> Why do you continue to relitigate 30-year-old decisions? If you don't want
> to put it in your rewrite, leave it out.

I would like to know if this decision is based on any logic reasoning.
But it seems to be more based on convenience of implementation as when
it is implemented in this way, the implementation for math can be
partly shared with other parts.

The reason that I want to understand the rationale is that things like
these seem to make shell code inherently hard to optimize for speed,
because there is no way to know what the expression expands to until
the code actually runs. If speed were a concern for the math
operations, then this feature seems better be stripped off or be
disabled by a shopt.

>> Is there an irrefutable reason that this syntax must have been like
>> this when it was first introduced? Thanks.
>
> Come on.

>> a=4
>> b=a
>> c=b
>> ((d = c * 2))
>> echo "$d"    # output: 8

How does the above code multi-level de-reference work below the
surface? Without (()), there is no such multi-level de-reference. So I
want to understand how it works.

Could anybody help explain?

Also, is there a real situation such multi-level reference is really
useful. For this specific case, it can be just rewritten as the
following. So the original code does not seem to be useful but just
introduces unnecessary complexity.

a=4
b=$a
c=$b
((d = c *2))

> `let' and $((...)) work the same way.

Just to be sure, `(())` and `let` are not part of POSIX but bash
extensions of POSIX shell?

-- 
Regards,
Peng



reply via email to

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