help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Bash-5.2 Release available


From: Greg Wooledge
Subject: Re: Bash-5.2 Release available
Date: Fri, 28 Oct 2022 21:55:10 -0400

On Fri, Oct 28, 2022 at 06:22:22PM -0700, Corey Hickey wrote:
> I'm using '@' for the variable as a deliberately pathological value.
> With bash 5.2 defaults, I can't find a way to make this work. Keeping
> the subscript variable escaped results in an evaluation to an undefined
> key (apparently):
> 
> $ ./bash -c 'declare -A assoc ; var=@ ; assoc[$var]=1 ; echo "$((1 + 
> assoc[\$var]))"'
> 1

The [\$key] workaround that works in previous versions of bash does not
work in 5.2.

https://mywiki.wooledge.org/BashPitfalls#pf62

  As of bash 5.2, the only safe, working way to modify an element of an
  associative array in a calculation is to make a temporary copy of the
  value in a regular (string) variable.

  tmp=${hash[$key]}
  ((tmp++))              # Safe.
  hash[$key]=$tmp

  # Or:
  tmp=${hash[$key]}
  hash[$key]=$((tmp+1))  # Safe.

I believe you're hitting the same issue here, even though you're only
trying to retrieve an associative array element in a math context,
rather than trying to modify one.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]