Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VirtualEnv initilaized from a bash script

I am trying to write what should be a super simple bash script. Basically activate a virtual env and than change to the working directory. A task i do a lot and condesing to one command just made sense.

Basically ...

#!/bin/bash
source /usr/local/turbogears/pps_beta/bin/activate
cd /usr/local/turbogears/pps_beta/src

However when it runs it just dumps back to the shell and i am still in the directory i ran the script from and the environment isn't activated.

like image 554
Ominus Avatar asked Aug 03 '11 15:08

Ominus


2 Answers

All you need to do is to run your script with the source command. This is because the cd command is local to the shell that runs it. When you run a script directly, a new shell is executed which terminates when it reaches the script's end of file. By using the source command you tell the shell to directly execute the script's instructions.

like image 79
Nicola Musatti Avatar answered Sep 30 '22 04:09

Nicola Musatti


The value of cd is local to the current script, which ends when you fall off the end of the file.

What you are trying to do is not "super simple" because you want to override this behavior.

Look at exec for replacing the current process with the process of your choice.

For feeding commands into an interactive Bash, look at the --rcfile option.

like image 25
tripleee Avatar answered Sep 30 '22 04:09

tripleee