4

Writing a script to automate my flask environment setup.

if [[ -z $1 ]];
    then
        echo "usage: flaskup <dirname> <template dir>";
        exit
    else
        virtualenv $1 &&
        cd ./$1 &&
        source bin/activate &&
        bin/pip install flask &&
        mkdir ./app &&
        mkdir ./app/static &&
        mkdir ./app/templates && 
        exit;
fi

I'm expecting this to leave me in the directory it created, with the virtual environment activated, however it leaves me in the same directory I ran the script from. What can I do to make the script exit with the shell in the activated virtual environment?

blanket_cat
  • 288
  • 2
  • 11

1 Answers1

2

If you run the script in its own shell (run it as /path/to/script or script if it lives in your $PATH) then you can't get what you want. The shell that runs the script is a different shell then the one you ran it from and it cannot change the status of the parent shell. The closest you could do would be to have the script echo the path as output and run it as cd "$(/path/to/script)" or similar.

Alternatively, if you run the script as . /path/to/script (or similar) then you are running it with your current shell and any directory changes it makes will be happening in your current shell and not a sub-shell.

Etan Reisner
  • 77,877
  • 8
  • 106
  • 148