This is my script (zsh 5.8):
# Consider parameter expansion of an unset variable to be an error
set -u
# Define and populate associative array
typeset -A arr
arr=(x xxx y yyy)
echo $arr[x] # Outputs xxx
# Test whether key x is present
((${arr[(Ie)x]})) && echo true
The last line gives me the error zsh: x: parameter not set, and from this message I conclude that zsh treats the x
not literally, but as a variable which zsh wants to expand. Why do I have a variable expansion here, and how can I write test (without violating set -u
) for checking that the key x is present in the array?
I know that I can of course write an explicit loop over the keys (i.e. for k in ${(k)arr}
, but I suspect that there must be an easier way to accomplish this.