« Return to Thread: status on $[arith] for eval arith vsl $((arith))??

Re: status on $[arith] for eval arith vsl $((arith))??

by Linda Walsh-3 :: Rate this Message:

| View in Thread



Chet Ramey wrote:

> On 4/9/12 9:07 PM, Linda Walsh wrote:
>>
>> ** linux-kernel -- all over the place...
>>
>> At that point, I was getting too many to keep up with ... so I stopped
>> searching...
>>
>> $[] has is -- I would bet, Universally, used MORE than $(())...
>
> I believe you'd lose, but it's unprovable either way.  Consider the fact,
> though, that bash and zsh support $[...], ksh93 and dash support only
> $((...)), but that all four support $((...)).

----

Well, I think you are probably right -- looks like alot of newer more
bash-specific, and 'clever' code uses (())...  but it isn't easy on the eyes...


But the examples of use of $(()) are leaning far more toward the perverse side
than simple use of $[]...

Maybe lisp programmers prefer (())??

a few examples:


- options="$(expr "${options}" : "\(.\{$((${last_option_index}-1))\}\)")"
- YESTERDAY=$(date -r $((`date +%s` - 86400 )) +%d/%m/%Y
- PREGAP=$(($(echo $OFFSETS | cut -f1 -d' ')))

- for ((vl=$((v_level - 1)); $vl > 0; --vl))

Using [] in expressions provides a distinct visual feature that is
different from the content....   There's no unclarity in confusing it
with $(xxx) $((xxx)) if xxx is a var and a function, which did they mean?

You don't hae that issue with $[], as to confuse it with an array, you'd
have to leave off the array name and the curly brackets...
======================================================================
Maarten Billemont wrote:

 > (Don't give

 >  me the spiel about how [...] is already arithmetic evaluation
 > inside array indices, that's a different syntax  entirely,

How is that?  I thought it WAS the syntax.

${xxx[yyy]}  => remove the array base, and you just get a calculated
offset as a result

=> $[yyy]...

It is similar to C in that regard.   You can put [xxx] after an ID, and it
means it is an array # of items past the array's start.  but if the array was
missing.  (well besides the syntax errors), it would just be the offset.



 >>> BTW, in case there is any doubt, I am not pushing for removal of $(()), or
a change of the status quo, but I am taking issue with the idea of removing $[],
as some proposed...




>> Chet -- you should get back to the posix folks and tell them posix is to be
>> 'descriptive of usage' (their words), not prescriptive.   Just because ksh
>> did it differently from everyone else's usage doesn't mean they should go
>> with that syntax...
>
> You have got to be kidding.  Don't you realize you're talking about
> decisions that are nearly 20 years old?  That $[...] was a Posix
> invention that only ever appeared in P1003.2d9?  That the $((...))
> syntax was adopted officially for P1003.2-1992?  That's 1992.  Twenty
> years ago.

====


> Now, it's hardly any problem to keep dragging the $[...] syntax along.
> It takes only a few dozen bytes of code.  I have no plans to remove it.
> But let's not kid ourselves: it's revisionist history to think that
> $[...] was widespread before Posix so callously stamped it out.

----
        I've seen it more often in portable code than the $(()) -- but I
could be remembering it more because it stands out too...


>
>> Geez.
>
> Indeed.

>

        Indubitably.
:-)


 « Return to Thread: status on $[arith] for eval arith vsl $((arith))??