title: How can a shell function know if it is running within a virtualenv?
body: <p>How should a <code>bash</code> function test whether it is running inside a Python virtualenv?</p>
  
  <p>The two approaches that come to mind are:</p>
  
  <pre><code>[[ "$(type -t deactivate)" != function ]]; INVENV=$?
  </code></pre>
  
  <p>or</p>
  
  <pre><code>[[ "x$(which python)" != "x$VIRTUAL_ENV/bin/python" ]]; INVENV=$?
  </code></pre>
  
  <p>(Note: wanting <code>$INVENV</code> to be 1 if we're inside a virtualenv, and 0 otherwise, is what forces the backward-looking tests above.)</p>
  
  <p>Is there something less hacky?</p>