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

Re: bad math expression: illegal character: "



> On 19 June 2020 at 07:38 Lawrence Velázquez <vq@xxxxxxxxx> wrote:
> Anyway, unless I'm missing something, POSIX seems pretty clear about
> this, as far as $((...)) goes:
> 
>     The expression shall be treated as if it were in double-quotes,
>     except that a double-quote inside the expression is not treated
>     specially. The shell shall expand all tokens in the expression
>     for parameter expansion, command substitution, and **quote
>     removal**. [Emphasis mine.]
> 
>     Next, the shell shall treat this as an arithmetic expression
>     and substitute the value of the expression.
> 
> https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_04

It's straightforward just to treat double quotes as if they're a
random spacing character.  Not sure the error is actually useful?
There was a test for it but that was simply to make sure we did
something with them.

pws

diff --git a/Src/math.c b/Src/math.c
index 905b910ec..b57ba42d4 100644
--- a/Src/math.c
+++ b/Src/math.c
@@ -831,6 +831,8 @@ zzlex(void)
 	case ' ': /* Fall through! */
 	case '\t':
 	case '\n':
+	case '"': /* POSIX says ignore these */
+	case Dnull:
 	    break;
 	default:
 	    if (idigit(*--ptr) || *ptr == '.')
diff --git a/Test/C01arith.ztst b/Test/C01arith.ztst
index 419f45292..d0092fefa 100644
--- a/Test/C01arith.ztst
+++ b/Test/C01arith.ztst
@@ -180,9 +180,10 @@
 1:bases beyond 36 don't work
 ?(eval):1: invalid base (must be 2 to 36 inclusive): 37
 
+  fail=39
   print $(( 3 + "fail" ))
-1:parse failure in arithmetic
-?(eval):1: bad math expression: operand expected at `"fail" '
+0:Double quotes are not treated specially in arithmetic
+>42
 
   alias 3=echo
   print $(( 3 + "OK"); echo "Worked")
@@ -487,3 +488,8 @@
   let noexist==0 )
 1:Arithmetic, NO_UNSET part 3
 ?(eval):2: noexist: parameter not set
+
+  print $(( "6+2" / "1+3" ))
+0:Double quotes are not treated specially in arithmetic (POSIX)
+# and do not do grouping!  this is 6 + (2/1) + 3
+>11



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