title: Dynamic variable created in function not available in future calls
body: <p>I have a script that is (supposed to be) assigning a dynamic variable name (s1, s2, s3, ...) to a directory path:</p>
  
  <pre><code>savedir() {
      declare -i n=1
      sn=s$n
      while test "${!sn}" != ""; do
          n=$n+1
          sn=s$n
      done
      declare $sn=$PWD
      echo "SAVED ($sn): ${!sn}"
  }
  </code></pre>
  
  <p>The idea is that the user is in a directory they'd like to recall later on and can save it to a shell variable by typing 'savedir'.  It -does- in fact write out the echo statement successfully:  if I'm in the directory /home/mrjones and type 'savedir', the script returns:
      SAVED (s1): /home/mrjones</p>
  
  <p>...and I can further type:</p>
  
  <blockquote>
    <p>echo $sn</p>
  </blockquote>
  
  <p>and the script returns:
      s1</p>
  
  <p>...but typing either...</p>
  
  <pre><code>&gt; echo $s1
  </code></pre>
  
  <p>...or </p>
  
  <blockquote>
    <p>echo ${!sn}</p>
  </blockquote>
  
  <p>...both return nothing (empty strings). What I want, in case it's not obvious, is this:</p>
  
  <blockquote>
    <p>echo $s1</p>
  </blockquote>
  
  <pre><code>/home/mrjones
  </code></pre>
  
  <p>Any help is greatly appreciated!  [apologies for the formatting...]</p>