title: pip installs packages in virtualenv and globally when called from bash script
body: <p>I am trying to activate a python environment and installing some packages using pip in a bash file. It succeeds to install in the environment but also installs globally on my mac, and I don't understand why.</p>
  
  <p>script.sh:</p>
  
  <pre class="lang-sh prettyprint-override"><code>#!/bin/bash
  source '&lt;absolute path&gt;/venv/bin/activate'
  which pip
  pip install psutil
  pip list
  </code></pre>
  
  <p>output:</p>
  
  <pre><code>$ ./script.sh
  &lt;absolute path&gt;/venv/bin/pip
  ...
  Package          Version  
  ---------------- ---------
  pip              19.0.3   
  psutil           5.6.3    
  setuptools       41.0.1  
  wheel            0.33.1   
  </code></pre>
  
  <p>But, in another terminal without the virtual environment it also gets installed:</p>
  
  <pre><code>$ which pip
  /usr/local/opt/python/libexec/bin/pip
  $ pip list
  Package          Version  
  ---------------- ---------
  pip              19.0.3   
  psutil           5.6.3   
  setuptools       41.0.1    
  wheel            0.33.1   
  </code></pre>
  
  <p>If i type the commands in the script one by one in the terminal, it works as expected.<br>
  What am i missing?</p>
  
  <p>edit:<br>
  If i run <code>pip freeze | xargs pip uninstall -y</code> (either in env or globally) it removes the packages both in the virtualenv and globally.<br>
  If i run the script again and I list the content in 'site-packages' <code>ls venv/lib/python3.7/site-packages</code> the installed package is not there, so my 'venv/bin/pip' is somehow pointing to my global 'site-packages'?</p>
  
  <p>edit2:
  I found a way to reproduce the behaviors.<br>
  1. create an environment <code>python -m venv venv_test</code><br>
  2. activate the environment <code>. venv_test/bin/activate</code><br>
  3. edit <code>source '&lt;absolute path&gt;/venv_test/bin/activate'</code> in script.sh<br>
  4. run script <code>./script.sh</code>  </p>
  
  <p>Why does this make pip install globally?</p>
  
  <p>If i skip 2. it works as expected.</p>