Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: for loop 'bad math expression'



On Sun, Feb 4, 2024 at 12:49 PM Lawrence Velázquez <larryv@xxxxxxx> wrote:
>
> (I don't know why all of a sudden you've switched from using "let"
> to not using it.)

I believe he's expecting assignments to work the same way on "numbers"
whether or not they are declared as integers.  This goes back to
expecting arithmetic to promote strings to integers.

For the rest of the audience ...

Using
  % let var=value
always applies math context, just as ((...)) and  $((...)) and $[...] do.

Bare assignments
  % var=value
apply math context only when "var" already has a numeric type.
Otherwise, the more usual case, it's string assignment (ignoring
arrays here).

Using typeset, declare, or local applies math context when an integer
option (-i, -F, etc.) is provided OR when var already has a numeric
type.

> > ... I suppose there's a good reason for it, but that leaves me dumbfounded.

Historically, I expect this results from lack in the base "sh" of
user-defined math functions.  That is, you can write

  func="a+b*c"
  let a=2 b=3 c=5
  print $((func))

to get 17.




Messages sorted by: Reverse Date, Date, Thread, Author