title: How to trim whitespace from a Bash variable?
body: <p>I have a shell script with this code:</p>
  
  <pre><code>var=`hg st -R "$path"`
  if [ -n "$var" ]; then
      echo $var
  fi
  </code></pre>
  
  <p>But the conditional code always executes, because <code>hg st</code> always prints at least one newline character.</p>
  
  <ul>
  <li>Is there a simple way to strip whitespace from <code>$var</code> (like <code>trim()</code> in <a href="http://en.wikipedia.org/wiki/PHP" rel="noreferrer">PHP</a>)?</li>
  </ul>
  
  <p>or</p>
  
  <ul>
  <li>Is there a standard way of dealing with this issue?</li>
  </ul>
  
  <p>I could use <a href="http://en.wikipedia.org/wiki/Sed" rel="noreferrer">sed</a> or <a href="http://en.wikipedia.org/wiki/AWK" rel="noreferrer">AWK</a>, but I'd like to think there is a more elegant solution to this problem.</p>