title: Local variables in bash: local vs subshell
body: <p>As far as I know there are two ways to create local variables in a bash function: create a subshell or declare every variable as local.</p>
  
  <p>For example:</p>
  
  <pre><code># using local
  function foo
  {
    local count
    for count in $(seq 10)
    do
      echo $count
    done
  }
  </code></pre>
  
  <p>or</p>
  
  <pre><code># using subshell
  function foo
  {
    (
      for count in $(seq 10)
      do
        echo $count
      done
    )
  }
  </code></pre>
  
  <p>Obvisously the version using the subshell is simpler to write because you don't have to care about declaring all variables local (not to mention (environment) variables created/exported by tools like <a href="http://en.wikipedia.org/wiki/Getopts" rel="noreferrer" title="getopts">getopts</a>). But I could imagine that creating a subshell has an overhead.</p>
  
  <p>So what is the better approach? What are the pros/cons?</p>