title: How to pip install into a virtualenv via a bash install script?
body: <p>I recently tried to automate the setup of an Ubuntu VM with a bash script (I am new to bash scripting).</p>
  
  <p>The issue is that the way I set it up, it doesn't work. Particularly the mkvirtualenv and workon commands don't work in the bash script. How do I create a virtualenv in a bash script with passing it a variable and then install into the virtualenv via pip?</p>
  
  <pre><code>#!/bin/bash
  VENV_NAME='name_of_virtualenv'
  
  #Setting up virtualenv
  mkdir --mode=770 /var/virtualenvs
  chown -R www-data:www-edit /var/virtualenvs
  chmod 771 /var/virtualenvs
  echo '# virtualenv and virtualwrapper' &gt;&gt; ~/.bashrc
  echo '  export VIRTUALENV_USE_DISTRIBUTE=1'  &gt;&gt; ~/.bashrc # &lt;-- Always use pip/distribute
  echo '  export WORKON_HOME=/var/virtualenvs'  &gt;&gt; ~/.bashrc
  echo '  source /usr/local/bin/virtualenvwrapper.sh'  &gt;&gt; ~/.bashrc
  echo '  export PIP_VIRTUALENV_BASE=$WORKON_HOME'  &gt;&gt; ~/.bashrc
  echo '  export PIP_RESPECT_VIRTUALENV=true'  &gt;&gt; ~/.bashrc
  source ~/.bashrc
  
  mkvirtualenv --distribute '{VENV_NAME}'
  workon {VENV_NAME}
  
  pip install psycopg2
  pip install --upgrade PIL
  </code></pre>