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: Chet Ramey
Subject: Re: Is it really necessary to allow operators and whole right hand sides to be substituted in (())?
Date: Tue, 18 May 2021 09:35:51 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Thunderbird/78.10.1

On 5/17/21 9:31 PM, Peng Yu wrote:

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.

David Korn is no longer active on the net, but the functional evolution is
pretty straightforward: from `let' using conventional shell expansions and
quoting, to `((' using the same parsing semantics while tinkering with
the syntax, to POSIX inventing `$((' and adopting the existing behavior.

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.

That's an important consideration, especially in an era where code size was
more important than it is now.

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.

The shell often defers parsing until runtime, with the philosophy of not
doing work until you have to. And unless you're going to fold the math
parser into the shell grammar -- or at least the lexer, since you have
existing requirements for quoted strings and the contents of $(( ... )) --
you're going to have to do something at runtime.


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.

I explained this in a separate message.


Could anybody help explain?

Also, is there a real situation such multi-level reference is really
useful.

Yes, people use variables to hold arithmetic expressions in a variety of
applications. Try it with conditional evaluation.

 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.

Yes, you can always show a trivial example is trivial.

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

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

Yes. POSIX invented $((...)) because the committee wanted some kind of
arithmetic evaluation in the shell and thought it would work better as
a word expansion, so they left out the pure evaluation commands. They
don't serve exactly the same purpose, but it's what we have now.

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



reply via email to

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