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