Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I really like virtualenv when working with python code and am wondering if there existed some tool which could emulate some of it when working in bash.

What I want is some cleanly reverseble way of setting environment variables by loading/unloading configurations.

I know about a lot of tools which do some magic when entering some directory, but I guess I wouldn't want to start from something like that.

share|improve this question
add comment

2 Answers

not sure if it's what you're looking for but I have this on my rc files:

mklab() {
    # creates small test environments for scripting
    local d=$(mktemp -d) && cd "$d" || return 1
    clear; bash --login --noprofile --norc
    cd - && rm -r "$d"
}; export -f mklab

it puts you in a temp directory with a clean shell to make tests and removes it afterwards.

virtualenv is a bad example for bash because here you don't "install" libraries, if you're looking for a way to easily switch between bash versions you should mention that on the question.

share|improve this answer
    
Oh yes, one does "install" "libraries": for example by sourcing sh files. –  Martian Jan 4 '12 at 10:55
    
nah that's just running code, it would be the same if you copy-pasted the sourced file in your terminal. It's not a library and it's not an installation, at most it would be a set of function declarations that you manually load everytime you run the shell. –  Samus_ Jan 4 '12 at 16:02
add comment

A naive attempt:

to load env:

shvirtualenv() ( source "$1"; VIRTUALENV="$1" bash; )

On exit one should save variables (or not if undesirable):

shvirtualenv_exit() {
    # one could ask here whether to save the env:
    declare -x > $VIRTUALENV
}

trap EXIT shvirtualenv_exit

CONS:

  • It does not handle inheritance well: if you want to source another env on save it will save all and further changes in parent env are not reflected.
  • Another problem is with multiple sessions (just open another terminal) - now you will end up with conflicts

TODO:

  • I would love to see separate history for the env.
  • per-project bashrc
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.