This is how array variable subscripts should expand. The demo code should run in bash or zsh. Array variables work similarly in mksh and most versions of ksh93 until recently. They have some bugs and missing features.
1 #!/usr/bin/env bash
2 if [[ -v ZSH_VERSION ]]; then
3 emulate ksh
4 zmodload zsh/system
5 elif [[ -v BASH_VERSION ]]; then
6 shopt -s lastpipe extglob expand_aliases
7 shopt -u {assoc,array}_expand_once 2>/dev/null
8 enable -f fdflags{,}
9 enable -f tee{,}
10 fi
11
12 function p {
13 printf ' %s ' "$1" >&3
14 IFS= read -rd "" t <&4
15 printf '%d\0' "$((t+1))" | tee /proc/self/fd/3 >&5
16 }
17
18 function f {
19 typeset -a a
20 typeset fd f='a[$(p x)0]'
21 {
22 if [[ -v BASH_VERSION ]]; then fdflags -s +nonblock 4 5
23 elif [[ -v ZSH_VERSION ]]; then sysopen -ro nonblock -u 4 /proc/self/fd/4; sysopen -wao nonblock -u 5 /proc/self/fd/5
24 fi
25 printf '%s\0' -1 >&5
26 (( $(p a)a[\$(p b)f$(p c)]$(p d) ))
27 } 3>&1 4<<<'' 4<$(flock 4)<(flock -w 1 4) 5>/proc/self/fd/4
28 echo
29 }
30
31 function main {
32 set +m
33 typeset BASH_COMPAT=51
34 f
35 unset -v BASH_COMPAT
36 f
37 echo
38 }
39 main
40 typeset -u SHELL=$SHELL
41 typeset -p "${SHELL##*/}_VERSION"
a 0 c 1 d 2 b 3 x 4 a 0 c 1 d 2 b 3 x 4 typeset ZSH_VERSION=5.9 a 0 c 1 d 2 b 3 x 4 a 0 c 1 d 2 b 3 x 4 declare -- BASH_VERSION="5.1.16(1)-release" a 0 c 1 d 2 b 3 x 4 a 0 c 1 d 2/proc/self/fd/9: line 25: $(p b)f: arithmetic syntax error: operand expected (error token is "$(p b)f") declare -- BASH_VERSION="5.3.0(1)-devel"