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

Re: for loop 'bad math expression'



On Sat, Feb 3, 2024 at 8:14 PM Ray Andrews <rayandrews@xxxxxxxxxxx> wrote:

0 /usr/share/info 0 % var=abc; let var+=2; echo $var
2

... it looks like the shell is simply throwing away 'abc' and starting afresh with an integer and then incrementing it.


That's not qutie what's happening. Arithmetic expressions interpret all variables as numbers, whether the variables are declared as integers or not. If a variable's value doesn't look like a number, it is interpreted as the name of another variable, which is then looked up recursively. If it gets to a variable that doesn't exist, the value is taken as 0.

So, when you do any sort of assignment inside a let or ((...)) or array subscript or any other arithmetic context, the variable being assigned will always come out of it with a numeric value. But unless you've done a declare -i or typeset -i, it will not be stored as an integer; it will be converted back to a string that just happens to be all digits. What's the difference? Well, let's continue your example:

zsh% var=abc; let var+=2; echo $var
2
zsh% var+=2; echo $var # note: no let
22

As you can see, outside of let, using += appended to the variable instead of doing arithmetic. Because it's still a string.

If you were to go in and declare it as an integer, then += would have its arithmetic meaning even outside of explicit arithmetic context:

zsh% typeset -i var
zsh% var+=2; echo $var
24

But that's only because of the typeset. It doesn't happen automatically.

--
Mark J. Reed <markjreed@xxxxxxxxx>


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