Easiest way to check for an index or a key in an array?
Using:
set -o nounset
Having an indexed array like:
myArray=( "red" "black" "blue" )What is the shortest way to check if element 1 is set?
I sometimes use the following:test "${#myArray[@]}" -gt "1" && echo "1 exists" || echo "1 doesn't exist"I would like to know if there's a preferred one.
How to deal with non-consecutive indexes?
myArray=() myArray[12]="red" myArray[51]="black" myArray[129]="blue"How to quick check that
51is already set for example?How to deal with associative arrays?
declare -A myArray myArray["key1"]="red" myArray["key2"]="black" myArray["key3"]="blue"How to quick check that
key2is already used for example?