On 10/18/22 3:55 PM, Glenn Jackman wrote:

Bash Version: 5.2
Patch Level: 0
Release Status: release

Description:
         an arithmetic comparison where an array index contains an array
element fails.

Repeat-By:
         in bash 5.2
                 $ coins[3]=10 i=3 C[5]=42 p=15
                 $ # comparison throws error
                 $ (( C[p - coins[i]] > 10)) && echo Y || echo N
                 bash: p - coins\[i\]: syntax error: invalid arithmetic
operator (error token is "\[i\]")
                 $ # but arithmetic expression is OK
                 $ echo $(( C[p - coins[i]] ))
                 42

Thanks for the report. It's not the array reference that triggers it, but
the arithmetic comparison. If you'd added the comparison to the arithmetic
expansion, you'd have seen the same error.

The cause is the new code in bash-5.2 to prevent multiple evaluation of
array subscripts. The fix is to exempt the presence of `<' and `>' from
that treatment, since they're not treated specially in that context.

I've attached the trivial patch.

Chet

--
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    c...@case.edu    http://tiswww.cwru.edu/~chet/
*** ../bash-20221015/subst.c    2022-10-18 10:47:33.000000000 -0500
--- subst.c     2022-10-20 11:41:07.000000000 -0500
***************
*** 3825,3828 ****
--- 3825,3832 ----
  #endif
  
+ /* We don't perform process substitution in arithmetic expressions, so don't
+    bother checking for it. */
+ #define ARITH_EXP_CHAR(s) (s == '$' || s == '`' || s == CTLESC || s == '~')
+ 
  /* If there are any characters in STRING that require full expansion,
     then call FUNC to expand STRING; otherwise just perform quote
***************
*** 4034,4038 ****
    while (string[i])
      {
!       if (EXP_CHAR (string[i]))
        break;
        else if (string[i] == '\'' || string[i] == '\\' || string[i] == '"')
--- 4038,4042 ----
    while (string[i])
      {
!       if (ARITH_EXP_CHAR (string[i]))
        break;
        else if (string[i] == '\'' || string[i] == '\\' || string[i] == '"')

Reply via email to