On Fri, Jun 13, 2025 at 08:14:51 -0600, Stan Marsh wrote:
> >According to <https://mywiki.wooledge.org/BashPitfalls#pf62>:
> 
> I can't access that URL - get "Bad Gateway" (in at least two different 
> browsers)

Sometimes if there are too many requests all at once, it can get a
bit cranky.  Wait a few minutes and try again, is all I can suggest.

> >    (( hash[$key]++ ))    # is not safe
> >    let 'hash[$key]++'    # is safe
> 
> Could you explain what the issue/problem is?  What makes it unsafe?
> 
> And, I really don't like the sound of "It varies by version of bash"...

I already showed this.

bash-5.2:

hobbit:~$ unset hash
hobbit:~$ declare -A hash; key=\'\]
hobbit:~$ (( hash[$key]++ ))
hobbit:~$ declare -p hash
declare -A hash=(["']"]="1" )

bash-5.1:

hobbit:~$ bash-5.1
hobbit:~$ declare -A hash; key=\'\]
hobbit:~$ (( hash[$key]++ ))
bash-5.1: ((: hash[']]++ : bad array subscript (error token is "hash[']]++ ")
hobbit:~$ let 'hash[$key]++'
hobbit:~$ declare -p hash
declare -A hash=(["']"]="1" )

The issue is that there are values of $key for which (( hash[$key]++ ))
breaks, in various ways, in various versions of bash.  There are some
workarounds for this, but the workaround that we *used* to promote
as the best one available stopped working in bash 5.2.

So now we're promoting let 'hash[$key]++' as the best workaround, unless
that one also breaks in some future version.  If that happens, then
we'll be reduced to tmp=${hash[$key]}; let tmp++; hash[$key]=$tmp
which seems to be the only workaround that will probably never break.

Reply via email to