title: In Bash test if associative array is declared
body: <p>How can I test if an associative array is declared in Bash? I can test for a variable like:</p>
  <pre><code>[ -z $FOO ] &amp;&amp; echo nope
  </code></pre>
  <p>but I doesn't seem to work for associative arrays:</p>
  <pre><code>$ unset FOO
  $ declare -A FOO
  $ [ -z $FOO ] &amp;&amp; echo nope
  nope
  $ FOO=([1]=foo)
  $ [ -z $FOO ] &amp;&amp; echo nope
  nope
  $ echo ${FOO[@]}
  foo
  </code></pre>
  <p><strong>EDIT:</strong></p>
  <p>Thank you for your answers, both seem to work so I let the speed decide:</p>
  <pre><code>$ cat test1.sh
  #!/bin/bash
  for i in {1..100000}; do
      size=${#array[@]}
      [ &quot;$size&quot; -lt 1 ] &amp;&amp; :
  done
  $ time bash test1.sh #best of five
  
  real    0m1.377s
  user    0m1.357s
  sys     0m0.020s
  </code></pre>
  <p>and the other:</p>
  <pre><code>$ cat test2.sh
  #!/bin/bash
  
  for i in {1..100000}; do
      declare -p FOO &gt;/dev/null 2&gt;&amp;1 &amp;&amp; :
  done
  $ time bash test2.sh #again, the best of five
  
  real    0m2.214s
  user    0m1.587s
  sys     0m0.617s
  </code></pre>
  <p><strong>EDIT 2:</strong></p>
  <p>Let's speed compare Chepner's solution against the previous ones:</p>
  <pre><code>#!/bin/bash
  
  for i in {1..100000}; do
      [[ -v FOO[@] ]] &amp;&amp; :
  done
  $ time bash test3.sh #again, the best of five
  
  real    0m0.409s
  user    0m0.383s
  sys     0m0.023s
  </code></pre>
  <p>Well that was fast.</p>
  <p>Thanks again, guys.</p>