1

I recently tried to automate the setup of an Ubuntu VM with a bash script (I am new to bash scripting).

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?

#!/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' >> ~/.bashrc
echo '  export VIRTUALENV_USE_DISTRIBUTE=1'  >> ~/.bashrc # <-- Always use pip/distribute
echo '  export WORKON_HOME=/var/virtualenvs'  >> ~/.bashrc
echo '  source /usr/local/bin/virtualenvwrapper.sh'  >> ~/.bashrc
echo '  export PIP_VIRTUALENV_BASE=$WORKON_HOME'  >> ~/.bashrc
echo '  export PIP_RESPECT_VIRTUALENV=true'  >> ~/.bashrc
source ~/.bashrc

mkvirtualenv --distribute '{VENV_NAME}'
workon {VENV_NAME}

pip install psycopg2
pip install --upgrade PIL

1 Answer 1

1

You missing dollar before the call, also did not see an export for VENV_NAME

export VIRTUALENV_USE_DISTRIBUTE=1
echo {VIRTUALENV_USE_DISTRIBUTE}
{VIRTUALENV_USE_DISTRIBUTE}

echo ${VIRTUALENV_USE_DISTRIBUTE}
1

Unsure why you need to export out to bashrc and from the looks of it each time you run it, it would add the same exports to bashrc which will end up with a larger and larger bashrc file each time

Why not just make them local variables like

VIRTUALENV_USE_DISTRIBUTE=1

workon $VIRTUALENV_USE_DISTRIBUTE
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.