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: Dennis Williamson
Subject: Re: Is it really necessary to allow operators and whole right hand sides to be substituted in (())?
Date: Mon, 17 May 2021 17:11:57 -0500

On Mon, May 17, 2021, 4:09 PM Peng Yu <pengyu.ut@gmail.com> 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. If people really want to substitute
> operators, it can be done with eval.
>
> $ plus=+; eval "((x = 1 $plus 2))"; declare -p x
> declare -x x="3"
>
> Substituting operands is reasonable.
>
> $ x=1; y=2; ((z=$x + $y)); declare -p z
> declare -- z="3"
>
> Substituting the how expression seems to be unnecessary and can be
> done with eval as well.
>
> $ x='1+2'; ((z=$x)); declare -p z
> declare -- z="3"
>
> If I just read `((z=$x))` without knowing its context, it looks like
> just a regular assignment, I would not expect that there is
> computation going on within `$x`. In this sense, this syntax can allow
> unreadable code.
>
> So it would be better not to allow it when this syntax first appeared?
> Is there an irrefutable reason that this syntax must have been like
> this when it was first introduced? Thanks.
>
> --
> Regards,
> Peng
>


You do know there's more to it than that -  substitutions are chained, too,
right?

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


reply via email to

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