I want to work with associative arrays in bash, and I need to be able to check whether a key is present in the array or not.
I've found this tutorial: https://www.artificialworlds.net/blog/2012/10/17/bash-associative-array-examples
This is the relevant part: (Note that one has to do declare -A MYMAP
first)
$ MYMAP[foo]=bar
$ echo ${MYMAP[missing]} # Accessing a missing value gives ""
$ # Testing whether a value is missing from an associative array
$ if [ ${MYMAP[foo]+_} ]; then echo "Found"; else echo "Not found"; fi
Found
$ if [ ${MYMAP[missing]+_} ]; then echo "Found"; else echo "Not found"; fi
Not found
Never in my life have I seen the +_
in bash before, a search for "+_" bash
on DDG and SO gives nothing.
What is the +_
for?
I've tried the examples with and without +_
- Both yield the same results in my testings.