title: Test for non-zero length string in Bash: [ -n "$var" ] or [ "$var" ]
body: <p>I've seen Bash scripts test for a non-zero length string in two different ways. Most scripts use the <code>-n</code> option:</p>
  
  <pre><code>#!/bin/bash
  # With the -n option
  if [ -n "$var" ]; then
    # Do something when var is non-zero length
  fi
  </code></pre>
  
  <p>But the -n option isn't really needed:</p>
  
  <pre><code># Without the -n option
  if [ "$var" ]; then
    # Do something when var is non-zero length
  fi
  </code></pre>
  
  <p>Which is the better way?</p>
  
  <p>Similarly, which is the better way for testing for zero-length:</p>
  
  <pre><code>if [ -z "$var" ]; then
    # Do something when var is zero-length
  fi
  </code></pre>
  
  <p>or</p>
  
  <pre><code>if [ ! "$var" ]; then
    # Do something when var is zero-length
  fi
  </code></pre>