help-bash
[Top][All Lists]
Advanced

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

Re: operator precedence confusion


From: Greg Wooledge
Subject: Re: operator precedence confusion
Date: Mon, 29 Apr 2024 15:52:18 -0400

On Mon, Apr 29, 2024 at 02:43:32PM -0500, Mike McClain wrote:
> $ help let says:
> The following list of operators is grouped into levels of equal-precedence 
> operators.
> The levels are listed in order of decreasing precedence.
> 
>         id++, id--      variable post-increment, post-decrement
>         ++id, --id      variable pre-increment, pre-decrement
>         -, +            unary minus, plus
>         !, ~            logical and bitwise negation
>         **              exponentiation
>         *, /, %         multiplication, division, remainder
>         .
>         .
>         .
> 
> Hence plus(+) has higher precedence than multiplication(*).
> Yet
> $ let n=$[1+2*3]; echo $n;
> 7
> or
> $  echo $[1+2*3]
> 7
> 
> Shows that multiplication(*) is executed/interpreted before plus(+).

You're confusing "unary minus, plus" with "addition, subtraction".
Unary minus and unary plus are just numbers with a leading sign.

Here's a more complete excerpt from the "help let" output:

        id++, id--      variable post-increment, post-decrement
        ++id, --id      variable pre-increment, pre-decrement
        -, +            unary minus, plus
        !, ~            logical and bitwise negation
        **              exponentiation
        *, /, %         multiplication, division, remainder
        +, -            addition, subtraction
        <<, >>          left and right bitwise shifts
        <=, >=, <, >    comparison
        ==, !=          equality, inequality


Also, the $[ ] syntax has been deprecated for decades.  You should be
using $(( )) or let instead.



reply via email to

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