title: Why doesn't [[ ... ]] work when script called with sh, while [ ... ] works always?
body: <p>Script test.sh:</p>
  
  <pre><code>#!/bin/bash
  if [[ $# -eq 0 ]]; then
    echo "no arg"
  else
    echo "have arg"
  fi
  </code></pre>
  
  <p>When I ran it as </p>
  
  <pre><code>./test.sh
  </code></pre>
  
  <p>it said "no arg", which was expected, but if I run it as</p>
  
  <pre><code>sh ./test.sh
  </code></pre>
  
  <p>it prints "have arg", but it you print $#, it's zero in both cases.</p>
  
  <p>However, the script below</p>
  
  <pre><code>#!/bin/bash
  if [ $# -eq 0 ]; then
    echo "no arg"
  else
    echo "have arg"
  fi
  </code></pre>
  
  <p>prints "no arg" in both cases. </p>
  
  <p>Could somebody explain this? Why does <code>[[ ... ]]</code> interpret <code>$#</code> differently from <code>[ ... ]</code>?</p>
  
  <p>The explanations I've read about <code>[[ ... ]]</code> weren't clear enough about this.</p>