1

I am trying to put a source command in a bash script so that I can quickly set up and use a virtual environment when writing django websites.

I tried the following without much success as my path was not prefixed with the (path) like it does when I simply enter it at the prompt.

#!/bin/bash
current=$(pwd | cut -d'/' -f5)
source ~/Documents/virtual-env/$current/bin/activate

Can anybody help and let me know what I am overlooking?

EDIT:

pwd is "example" and the source is:
"~/Documents/virtual-env/example/bin/activate".

After some research I think I need to use something like:
"source ./script"

(not working) as I think the environment is created but not esculated to its parent enviroment which I believe is not possible now.

6
  • What is the output of pwd from within the script, and what would be the corresponding path that you are trying to source? Show at least one example. Commented Sep 28, 2017 at 10:07
  • pwd is "example" and the source is "~/Documents/virtual-env/example/bin/activate". After some research I think I need to use something like "source ./script" (not working) as I think the environment is created but not esculated to its parent enviroment which I believe is not possible now. Commented Sep 28, 2017 at 10:18
  • 2
    Yeah you can't change the parent environment by running a script (which happens in a child process), you need to source it. But it's still unclear exactly what you're trying to do. Commented Sep 28, 2017 at 10:55
  • Note that you need to source your script for the same reason it needs to source, not execute, the activate script. Commented Sep 28, 2017 at 11:00
  • What does "without much success" mean, exactly? Did you get an error? What are the exact paths involved? It's very difficult to diagnose when we have such limited information. Commented Sep 28, 2017 at 12:56

1 Answer 1

1
#!/bin/bash
current=$(basename $(pwd))
source ~/Documents/virtual-env/$current/bin/activate
exec bash # Run new interactive shell in the new environment

But I recommend to try virtualenvwarpper instead.

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.