title: bash functions: enclosing the body in braces vs. parentheses
body: <p>Usually, bash functions are defined using curly braces to enclose the body:</p>
  
  <pre><code>foo()
  {
      ...
  }
  </code></pre>
  
  <p>When working on a shell script today making extensive use of functions, I've run into problems with variables that have the same name in the called as in the calling function, namely that those variables are the same. I've then found out that this can be prevented by defining the local variables inside the function as local: <code>local var=xyz</code>.</p>
  
  <p>Then, at some point, I've discovered a thread (<a href="https://stackoverflow.com/questions/14651980/defining-bash-function-body-using-parenthesis-instead-of-braces">Defining bash function body using parenthesis instead of braces</a>) in which it is explained that it's just as valid to define a function using parentheses like this:</p>
  
  <pre><code>foo()
  (
      ...
  )
  </code></pre>
  
  <p>The effect of this is that the function body is executed in a subshell, which has the benefit that the function has its own variable scope, which allows me to define them without local. Since having a function-local scope seems to make much more sense and to be much safer than all variables being global, I immediately ask myself:</p>
  
  <ul>
  <li><strong>Why are braces used by default to enclose the function body instead of parentheses?</strong></li>
  </ul>
  
  <p>However, I quickly also discovered a major downside to executing the function in a subshell, specifically that exiting the script from inside a function doesn't work anymore, instead forcing me to work with the return status along the whole call tree (in case of nested functions). This leads me to this follow-up question:</p>
  
  <ul>
  <li><strong>Are there other major downsides (*) to using parentheses instead of braces (which might explain why braces seem to be preferred)?</strong></li>
  </ul>
  
  <p><em>(*) I'm aware (from exception-related discussions I've stumbled upon over time) that some would argue that explicitly using the error status is much better than being able to exit from anywhere, but I prefer the latter.</em></p>
  
  <p>Apparently both styles have their advantages and disadvantages. So I hope some of you more experienced bash users can give me some general guidance:</p>
  
  <ul>
  <li><strong>When shall I use curly braces to enclose the function body, and when is it advisable to switch to parentheses?</strong></li>
  </ul>
  
  <p><strong>EDIT: Take-aways from the answers</strong></p>
  
  <p>Thanks for your answers, my head's now a bit clearer with regards to this. So what I take away from the answers is:</p>
  
  <ul>
  <li><p>Stick to the conventional curly braces, if only in order not to confuse potential other users/developers of the script (and even use the braces if the whole body is wrapped in parentheses).</p></li>
  <li><p>The only real disadvantage of the curly braces is that any variable in the parent scope can be changed, although in some situations this might be an advantage. This can easily be circumvented by declaring the variables as <code>local</code>.</p></li>
  <li><p>Using parentheses, on the other hand, might have some serious unwanted effects, such as messing up exits, leading to problems with killing a script, and isolating the variable scope.</p></li>
  </ul>