title: Bash array parameter expansion within parameter name: bad substitution
body: <p>I wrote a script in which I want the user to input a directory or many directories and each is checked for something unimportant to this discussion. All interior directories are also checked up to a specified depth.</p>
  
  <p>While I can declare the array <code>Directories0</code> (the input directories) to start, I cannot refer to it in any way... result: bad substitution. Obviously, <code>Directories1</code> would be depth=1, <code>Directories2</code> is depth=2, and so on...</p>
  
  <p><strong>Below is a snippet of code:</strong></p>
  
  <pre><code>let Recurse=4               ## say... variable value ##
  [ "$Recurse" ] &amp;&amp; let MaxDepth="$Recurse" || let MaxDepth=0
  declare -i depth=0
  
  IFS=$'\n'
  ## declare -a Directories${depth}=("${@}")      ##  &lt;—— doesn't work
  ## declare -a Directories${depth}="("${@}")"        ##  &lt;—— works if the brackets only are quoted...
  ## declare -a Directories${depth}=\("${@}"\)        ##  &lt;—— ... or escaped
  declare -a "Directories${depth}=("${@}")"
  IFS=$' \t\n'
  
  ## Nested loop, depth counter increases for each directory depth. I want to stop at a specific depth which is entered as an option ##
  for (( depth = 0; depth &lt;= MaxDepth; depth++ )); do                  ## MaxDepth is entered as option ##
      until [ -z "${Directories${depth}[*]}" ]; do                       ## ***** bad substitution error ***** ##
          declare input="$(follow "${Directories${depth}[0]}")"       ## follow is a script that resolves symlinks and Finder aliases ##
          CheckDirectory "${input%/}/"                                  ## check directory ##
          case $? in
  
          ## Tests passed ##
          0)  if [[ "$Recurse" &amp;&amp; "$depth" -lt "$MaxDepth" ]]; then
                  IFS=$'\n'
                  ## get ready to check sub-directories ##
                  declare -a Directories$(( depth + 1 ))="("${Directories$(( depth + 1 ))[@]}" $(find -P "${Directories${depth}[0]}" -type d -mindepth 1 -maxdepth 1 -exec follow '{}' \;))"
                  IFS=$' \t\n'
              fi
              true;;
  
          ## Tests failed ##
          *)  false;;
          esac
          [ $? -eq 0 ] &amp;&amp; unset Directories${depth}[0] || exit 1             ## if test fails, exit, if succeeds, move on to next directory ##
          declare -a Directories${depth}="("${Directories${depth}[@]}")"      ## re-shuffle the array to get rid of null value at index 0 ##
          (( element++ ))
      done
  done
  </code></pre>
  
  <p><strong>Below is a simplified version in case you don't want to go through the code above, this is the crux of the problem:</strong></p>
  
  <pre><code>depth=2
  declare -a "Directories${depth}=(yo man ma me mo)"
  echo "${Directories${depth}[4]}"
      &gt; -bash: ${Directories${depth}[4]}: bad substitution
  echo "${Directories2[4]}"
      &gt; mo
  </code></pre>
  
  <p>Solutions, anyone?</p>