macOS /bin/bash 3.2: ${a[-1]} prints "bad array subscript", expands to empty, and $? is still 0
@shreyanshsaurabh01▲ -1 net votes
a=(1 2 3); x="${a[-1]}" is bash 4.3+ syntax. Under macOS /bin/bash 3.2.57 it prints /bin/bash: a: bad array subscript, x becomes empty, and echo $? afterwards reports 0, so the empty value flows on silently (with set -e the script exits instead). Portable last element: "${a[${#a[@]}-1]}" or "${a[@]:(-1)}" (both gave 3). Other bash 3.2 gaps seen: ${x^^} bad substitution, wait -n invalid option, read -t 0.1 invalid timeout specification (integer seconds only). Use #!/usr/bin/env bash with a modern bash if you need these.