title: Why do the `man bash` pages state the `declare` and `local` `-n` attribute "cannot be applied to array variables", and yet it can?
body: <p>Why does <code>local -n</code> work on array variables when the manual <em>explicitly says it does not</em>? Is the manual wrong? Is this relying on some sort of bash &quot;undefined behavior?&quot; Is the manual out-of-date? Am I missing something?</p>
  <p>Here is the information I'm looking at from the bash manual:</p>
  <p>Run <code>man bash</code> and search for <code>local [</code> using the regular expression search pattern <code>local \[</code>. It says (emphasis added):</p>
  <blockquote>
  <p><code>local [option] [name[=value] ... | - ]</code></p>
  <p>For each argument, a local variable named name is created, and assigned  value.  <strong>The
  option  can  be  any of the options accepted by <code>declare</code>.</strong></p>
  </blockquote>
  <p>(See also <code>help local</code>).</p>
  <p>So, the options that can be passed to the bash <code>local</code> built-in command are the same as those of <code>declare</code>. Let's find what options can be passed to <code>declare</code>:</p>
  <p>Run <code>man bash</code> and search for <code>declare [</code> using the regular expression search pattern <code>declare \[</code>. Under the <code>-n</code> entry there for <code>declare [-aAfFgilnrtux] [-p] [name[=value] ...]</code> you'll see (emphasis added):</p>
  <blockquote>
  <p><code>-n</code>     Give each name the nameref attribute, making it a name  reference  to  another
  variable.   That  other  variable is defined by the value of name.  All references, assignments, and attribute modifications to name, except those using or
  changing  the <code>-n</code> attribute itself, are performed on the variable referenced by
  name's value.  <strong>The nameref attribute cannot be applied to array variables.</strong></p>
  </blockquote>
  <p>(See also <code>help declare</code>).</p>
  <p>So, despite it saying <strong>&quot;The nameref attribute cannot be applied to array variables,&quot;</strong>, it <strong><em>does</em> work just fine on array variables!</strong></p>
  <p>To prove that, here is a demo of it working fine for <strong>regular bash arrays</strong>:</p>
  <pre class="lang-sh prettyprint-override"><code>function foo {
      # declare a local **reference variable** (hence `-n`) named `data_ref`
      # which is a reference to the value stored in the first parameter
      # passed in
      local -n data_ref=&quot;$1&quot;
      echo &quot;${data_ref[0]}&quot;
      echo &quot;${data_ref[1]}&quot;
  }
  
  # declare a regular bash &quot;indexed&quot; array
  declare -a data
  data+=(&quot;Fred Flintstone&quot;)
  data+=(&quot;Barney Rubble&quot;)
  foo &quot;data&quot;
  </code></pre>
  <p>Sample output:</p>
  <blockquote>
  <pre><code>Fred Flintstone
  Barney Rubble
  </code></pre>
  </blockquote>
  <p>...and here is it working just fine on <strong>associative bash arrays</strong> (ie: bash hash tables, &quot;dictionaries&quot;, or &quot;unordered maps&quot;):</p>
  <pre class="lang-sh prettyprint-override"><code>function foo {
      # declare a local **reference variable** (hence `-n`) named `data_ref`
      # which is a reference to the value stored in the first parameter
      # passed in
      local -n data_ref=&quot;$1&quot;
      echo &quot;${data_ref[&quot;a&quot;]}&quot;
      echo &quot;${data_ref[&quot;b&quot;]}&quot;
  }
  
  # declare a bash associative array
  declare -A data
  data[&quot;a&quot;]=&quot;Fred Flintstone&quot;
  data[&quot;b&quot;]=&quot;Barney Rubble&quot;
  foo &quot;data&quot;
  </code></pre>
  <p>Sample output:</p>
  <blockquote>
  <pre><code>Fred Flintstone
  Barney Rubble
  </code></pre>
  </blockquote>
  <p>My <code>bash --version</code> is <code>4.4.20(1)-release</code>, on Linux Ubuntu 18.04:</p>
  <blockquote>
  <pre><code>GNU bash, version 4.4.20(1)-release (x86_64-pc-linux-gnu)
  Copyright (C) 2016 Free Software Foundation, Inc.
  License GPLv3+: GNU GPL version 3 or later &lt;http://gnu.org/licenses/gpl.html&gt;
  
  This is free software; you are free to change and redistribute it.
  There is NO WARRANTY, to the extent permitted by law.
  </code></pre>
  </blockquote>
  <h2>References:</h2>
  <ol>
  <li>I modified the above code samples from @Todd Lehman's answer here: <a href="https://stackoverflow.com/a/55170447/4561887">How to pass an associative array as argument to a function in Bash?</a></li>
  </ol>
  <p><em>Note: in case anyone is searching for it, this question itself also answers the questions: &quot;How do I pass a bash array as a parameter to a function?&quot; and &quot;How do I pass a bash associative array as a parameter to a function?&quot;</em></p>