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

Re: Variable fails to increment with lvalue error



> On 23/10/2023 16:33 Roman Perepelitsa <roman.perepelitsa@xxxxxxxxx> wrote:
> On Mon, Oct 23, 2023 at 5:28 PM Evan Clearfield <efclear@xxxxxxxxx> 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.




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