* Variable fails to increment with lvalue error @ 2023-10-23 15:27 Evan Clearfield 2023-10-23 15:33 ` Roman Perepelitsa 2023-10-23 16:34 ` Dominik Vogt 0 siblings, 2 replies; 6+ messages in thread From: Evan Clearfield @ 2023-10-23 15:27 UTC (permalink / raw) To: zsh-users I’m in the process of creating a shell script on macOS Sonoma. I’m just implementing a simple counter in a for loop. I initialized my variable outside the loop, such that “count=0”. I then increment it with a simple call “(($count++))”, and when I run the script, I receive the error “bad math expression: lvalue required” Any idea what I’m doing wrong? I’ve searched high and low, tried multiple variations of the “((count++))” expression, changed my initial number, etc. Nothing seems to work. HELP Sent from my iPhone ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Variable fails to increment with lvalue error 2023-10-23 15:27 Variable fails to increment with lvalue error Evan Clearfield @ 2023-10-23 15:33 ` Roman Perepelitsa 2023-10-23 16:43 ` Peter Stephenson 2023-10-23 16:34 ` Dominik Vogt 1 sibling, 1 reply; 6+ messages in thread From: Roman Perepelitsa @ 2023-10-23 15:33 UTC (permalink / raw) To: Evan Clearfield; +Cc: zsh-users On Mon, Oct 23, 2023 at 5:28 PM Evan Clearfield <efclear@gmail.com> wrote: > > I then increment it with a simple call “(($count++))”, and when I run the script, I receive the error “bad math expression: lvalue required” `$count` expands to `0`, so `(($count++))` is equivalent to `((0++))`. The post-increment operator (++) requires something that can be incremented (an lvalue). `0` is not an lvalue, so you get an error. What you want instead is `((count++))`. This will increment `count` as you expect. Roman. ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Variable fails to increment with lvalue error 2023-10-23 15:33 ` Roman Perepelitsa @ 2023-10-23 16:43 ` Peter Stephenson 2023-10-23 17:03 ` Roman Perepelitsa 0 siblings, 1 reply; 6+ messages in thread From: Peter Stephenson @ 2023-10-23 16:43 UTC (permalink / raw) To: zsh-users > On 23/10/2023 16:33 Roman Perepelitsa <roman.perepelitsa@gmail.com> wrote: > On Mon, Oct 23, 2023 at 5:28 PM Evan Clearfield <efclear@gmail.com> wrote: > > > > I then increment it with a simple call “(($count++))”, and when I run the script, I receive the error “bad math expression: lvalue required” > > `$count` expands to `0`, so `(($count++))` is equivalent to `((0++))`. Is it worth drawing attention to this, or is this just excess verbiage? pws diff --git a/Doc/Zsh/arith.yo b/Doc/Zsh/arith.yo index bc3e35ad5..e6380b2cb 100644 --- a/Doc/Zsh/arith.yo +++ b/Doc/Zsh/arith.yo @@ -23,8 +23,10 @@ command which begins with a `tt(LPAR()LPAR())', all the characters until a matching `tt(RPAR()RPAR())' are treated as a double-quoted expression and arithmetic expansion performed as for an argument of tt(let). More precisely, `tt(LPAR()LPAR())var(...)tt(RPAR()RPAR())' is equivalent to -`tt(let ")var(...)tt(")'. The return status is 0 if the arithmetic value -of the expression is non-zero, 1 if it is zero, and 2 if an error occurred. +`tt(let ")var(...)tt(")': note the presence of double quotes, so that variable +expansion etc. takes place before arithmetic evaluation. The return +status is 0 if the arithmetic value of the expression is non-zero, 1 if +it is zero, and 2 if an error occurred. For example, the following statement @@ -37,6 +39,10 @@ example(let "val = 2 + 1") both assigning the value 3 to the shell variable tt(val) and returning a zero status. +Note further that it would be incorrect to replace tt(val) with tt($val) +as the latter is affected by double quote substitution, so that +arithmetic evaluation does not see the name of the variable. + cindex(arithmetic base) cindex(bases, in arithmetic) Integers can be in bases other than 10. ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Variable fails to increment with lvalue error 2023-10-23 16:43 ` Peter Stephenson @ 2023-10-23 17:03 ` Roman Perepelitsa 2023-10-23 18:21 ` Ray Andrews 0 siblings, 1 reply; 6+ messages in thread From: Roman Perepelitsa @ 2023-10-23 17:03 UTC (permalink / raw) To: Peter Stephenson, Evan Clearfield; +Cc: zsh-users On Mon, Oct 23, 2023 at 6:44 PM Peter Stephenson <p.w.stephenson@ntlworld.com> wrote: > > > On 23/10/2023 16:33 Roman Perepelitsa <roman.perepelitsa@gmail.com> wrote: > > On Mon, Oct 23, 2023 at 5:28 PM Evan Clearfield <efclear@gmail.com> wrote: > > > > > > I then increment it with a simple call “(($count++))”, and when I run the script, I receive the error “bad math expression: lvalue required” > > > > `$count` expands to `0`, so `(($count++))` is equivalent to `((0++))`. > > Is it worth drawing attention to this, or is this just excess verbiage? > > pws > > diff --git a/Doc/Zsh/arith.yo b/Doc/Zsh/arith.yo > index bc3e35ad5..e6380b2cb 100644 > --- a/Doc/Zsh/arith.yo > +++ b/Doc/Zsh/arith.yo > @@ -23,8 +23,10 @@ command which begins with a `tt(LPAR()LPAR())', all the characters until a > matching `tt(RPAR()RPAR())' are treated as a double-quoted expression and > arithmetic expansion performed as for an argument of tt(let). More > precisely, `tt(LPAR()LPAR())var(...)tt(RPAR()RPAR())' is equivalent to > -`tt(let ")var(...)tt(")'. The return status is 0 if the arithmetic value > -of the expression is non-zero, 1 if it is zero, and 2 if an error occurred. > +`tt(let ")var(...)tt(")': note the presence of double quotes, so that variable > +expansion etc. takes place before arithmetic evaluation. The return > +status is 0 if the arithmetic value of the expression is non-zero, 1 if > +it is zero, and 2 if an error occurred. > > For example, the following statement > > @@ -37,6 +39,10 @@ example(let "val = 2 + 1") > both assigning the value 3 to the shell variable tt(val) and returning a > zero status. > > +Note further that it would be incorrect to replace tt(val) with tt($val) > +as the latter is affected by double quote substitution, so that > +arithmetic evaluation does not see the name of the variable. > + > cindex(arithmetic base) > cindex(bases, in arithmetic) > Integers can be in bases other than 10. > Let's ask the OP if this would've been helpful. Evan, have you read this documentation page? It's available at `man zshmisc` and online at https://zsh.sourceforge.io/Doc/Release/Arithmetic-Evaluation.html. If you have, would it have helped if the docs were amended as per Peter's patch? Roman. ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Variable fails to increment with lvalue error 2023-10-23 17:03 ` Roman Perepelitsa @ 2023-10-23 18:21 ` Ray Andrews 0 siblings, 0 replies; 6+ messages in thread From: Ray Andrews @ 2023-10-23 18:21 UTC (permalink / raw) To: zsh-users [-- Attachment #1: Type: text/plain, Size: 654 bytes --] On 2023-10-23 10:03, Roman Perepelitsa wrote: > Let's ask the OP if this would've been helpful. > > Evan, have you read this documentation page? It's available at Beeeutiful! Only the learner can say if a doc was useful, so ask him :-) My two cents would be: Note further that it would be incorrect to replace tt(val) with tt($val) as the latter is affected by the built-in double quote substitution, so that the arithmetic evaluation does not see the name of the variable but rather it's expanded value. Thus if $val = 0, the latter expression would attempt: 0 = 2 + 1 ... which is obviously nonsense. ... maybe too much hand-holding there? ... [-- Attachment #2: Type: text/html, Size: 1171 bytes --] ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Variable fails to increment with lvalue error 2023-10-23 15:27 Variable fails to increment with lvalue error Evan Clearfield 2023-10-23 15:33 ` Roman Perepelitsa @ 2023-10-23 16:34 ` Dominik Vogt 1 sibling, 0 replies; 6+ messages in thread From: Dominik Vogt @ 2023-10-23 16:34 UTC (permalink / raw) To: zsh-users On Mon, Oct 23, 2023 at 11:27:50AM -0400, Evan Clearfield wrote: > initialized my variable outside the loop, such that ???count=0???. > I then increment it with a simple call ???(($count++))???, and > when I run the script, I receive the error ???bad math expression: > lvalue required??? Use ((count++)) instead of (($count++)). Ciao Dominik ^_^ ^_^ -- Dominik Vogt ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2023-10-23 18:22 UTC | newest] Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2023-10-23 15:27 Variable fails to increment with lvalue error Evan Clearfield 2023-10-23 15:33 ` Roman Perepelitsa 2023-10-23 16:43 ` Peter Stephenson 2023-10-23 17:03 ` Roman Perepelitsa 2023-10-23 18:21 ` Ray Andrews 2023-10-23 16:34 ` Dominik Vogt
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).