From: "Mark J. Reed" <markjreed@gmail.com>
To: zsh-users@zsh.org
Subject: Re: for loop 'bad math expression'
Date: Sun, 4 Feb 2024 09:43:00 -0500 [thread overview]
Message-ID: <CAA=-s3wAcr--yxTbqxu0qEW1FEgg=ntVoP3j0mwOS_vEoB-ehA@mail.gmail.com> (raw)
In-Reply-To: <f228c732-d237-4597-b260-b28da72fd84b@eastlink.ca>
[-- Attachment #1: Type: text/plain, Size: 1630 bytes --]
On Sat, Feb 3, 2024 at 8:14 PM Ray Andrews <rayandrews@eastlink.ca> 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@gmail.com>
[-- Attachment #2: Type: text/html, Size: 3448 bytes --]
next prev parent reply other threads:[~2024-02-04 14:44 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-01-30 3:39 Ray Andrews
2024-01-30 3:46 ` Ray Andrews
2024-01-30 4:04 ` Bart Schaefer
2024-01-30 4:06 ` Bart Schaefer
2024-01-30 4:17 ` Ray Andrews
2024-01-30 13:44 ` Ray Andrews
2024-01-30 14:30 ` Lawrence Velázquez
2024-02-03 23:52 ` Bart Schaefer
2024-02-04 1:14 ` Ray Andrews
2024-02-04 2:05 ` Lawrence Velázquez
2024-02-04 4:20 ` Bart Schaefer
2024-02-04 16:08 ` Ray Andrews
2024-02-04 20:56 ` Lawrence Velázquez
2024-02-04 15:51 ` Ray Andrews
2024-02-04 20:48 ` Lawrence Velázquez
2024-02-04 21:09 ` Bart Schaefer
2024-02-04 21:23 ` Bart Schaefer
2024-02-05 2:10 ` Bart Schaefer
2024-02-05 2:43 ` Mikael Magnusson
2024-02-05 2:50 ` Bart Schaefer
2024-02-05 15:21 ` Ray Andrews
2024-02-05 15:48 ` Mark J. Reed
2024-02-04 14:43 ` Mark J. Reed [this message]
2024-02-04 16:37 ` Ray Andrews
2024-02-04 21:09 ` Lawrence Velázquez
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAA=-s3wAcr--yxTbqxu0qEW1FEgg=ntVoP3j0mwOS_vEoB-ehA@mail.gmail.com' \
--to=markjreed@gmail.com \
--cc=zsh-users@zsh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).