help-bash
[Top][All Lists]
Advanced

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

Re: whats wrong with (( a = 8 , a > 4 && a -= 2 || a-- )) , bash: ((: a


From: Bipul kumar
Subject: Re: whats wrong with (( a = 8 , a > 4 && a -= 2 || a-- )) , bash: ((: a = 8 , a > 4 && a -= 2 || a-- : attempted assignment to non-variable (error token is "-= 2 || a-- ")
Date: Thu, 23 Mar 2023 23:48:03 +0530

*$ (( a = 2 , a > 1 && (a -= 2) || a-- )); declare -p adeclare -- a="-1"*

The result -1 is expected.
When  a is  2 then 2> 1 is true AND  a = (2-2 = 0) Which is False as 0
consider false, so it moves to the OR part.


Respectfully,
    Bipul
    PUBLIC KEY <http://ix.io/1nWf>
    97F0 2E08 7DE7 D538 BDFA  B708 86D8 BE27 8196 D466
                    ** Please excuse brevity and typos. **


On Thu, Mar 23, 2023 at 4:52 PM Kerin Millar <kfm@plushkava.net> wrote:

> On Thu, 23 Mar 2023 11:32:33 +0100
> alex xmb ratchev <fxmbsw7@gmail.com> wrote:
>
> > i remember doing && (( code
> > maybe i didnt '=' in action there
> >
> > (( a = 8 , a > 4 && a -= 2 || a-- ))
> >
> > bash: ((: a = 8 , a > 4 && a -= 2 || a-- : attempted assignment to
> > non-variable (error token is "-= 2 || a-- ")
> >
> >
> > (( a = 8 , a > 4 && a-- && a-- || a-- ))
> >
> > works
> > a=6
> >
> > ..
> > i suppose this is a (( lex bug where u didnt include || && in op for =
>
> I agree that it looks like a possible bug in the parser. It works if
> parentheses are used to disambiguate one of the subtractions.
>
> $ (( a = 8 , a > 4 && (a -= 2) || a-- )); declare -p a
> declare -- a="6"
>
> Even when made to work, I would consider it to be poor. Consider what
> happens below. It is a fine example of what can happen in the course of
> writing unconventional code without understanding the consequences.
>
> $ (( a = 2 , a > 1 && (a -= 2) || a-- )); declare -p a
> declare -- a="-1"
>
> If you must do this sort of thing, use the conditional operator instead
> e.g. (( a = 8, a = (a > 4) ? a - 2 : a - 1 )). Alternatively, just avoid
> doing assignments in arithmetic expressions except where there's a clear
> advantage (I cannot see one in this case).
>
> --
> Kerin Millar
>
>


reply via email to

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