title: bash: no need to call popd after pushd at the end of a script?
body: <p>I was going through some trouble to do some proper "pushd/popd cleanup" in my bash script. That is: if I did <code>pushd</code> a few times, I wanted to make sure to do the same number of <code>popd</code> before <code>exit</code>.</p>
  
  <p>However, I noticed that the pushd/popd stack doesn't seem to be global.</p>
  
  <p>Suppose I have this script called <code>example-pushd-twice.sh</code> in my homedir:</p>
  
  <pre><code>pushd /etc
  pushd /tmp
  </code></pre>
  
  <p>And I do this from the shell from within my homedir: (resulting output in blockquotes)</p>
  
  <p><code>dirs</code> </p>
  
  <blockquote>
    <p>~</p>
  </blockquote>
  
  <p><code>./example-pushd-twice.sh</code></p>
  
  <blockquote>
    <p>/etc ~<br>
    /tmp /etc ~</p>
  </blockquote>
  
  <p>I now expected to still be in <code>/tmp</code> and still having a dir stack with 3 dirs, however:</p>
  
  <p><code>dirs</code> </p>
  
  <blockquote>
    <p>~</p>
  </blockquote>
  
  <p>Is this correct behavior? Can I trust a shell script to invoke its own pushd/popd stack, so I don't have to care about cleaning up afterwards?</p>