I need to run multiple sets of environments. For this, I have two sets of bashrc files, bashrc_a and bashrc_b. On startup, I made bashrc_a running by making .bashrc as

source bashrc_a

From bashrc_a environment, I need to switch to bashrc_b, but I don't want to involve any environment variables from bashrc_a remain in the bashrc_b environment. For this, I tried

env -i bash --rcfile bashrc_b

But the problem is, in this case, I lose all environment variables, not just those from bashrc_a. I have no HOME, I have no PATH, and so on.

Another thing I tried is

env -i bash --login --rcfile bashrc_b

But if I do this, login shell looks up for ~/.bashrc, then it looks up for bashrc_a file, and also, since TERM is deleted from env -i, it did not help.

So my question is, how do I unload bashrc_a and load bashrc_b?

  • Use environment-modules, it is designed for such tasks. You may need a bit more time to configure it first, but it is worth it. – Thomas Sep 16 '17 at 14:01

It sound, from what you describe, like you don't actually want to load the bashrc_b environment from bashrc_a.

Assuming that you dont't source either of bashrc_a or bashrc_b from .bashrc or .bash_profile.

  1. Start a bash shell as normal.
  2. Start the bashrc_a environment with bash --rcfile bashrc_a.
  3. If you want to switch to the bashrc_b environment
    1. first exit, and then
    2. bash --rcfile bashrc_b

If you want to keep the bashrc_a environment around, then just open another terminal and do bash --rcfile bashrc_b.

The other alternative would be to write a corresponding "unload" rc-file for each of the environments. These two files would unset or re-set the environment in such a way that they un-did whatever the corresponding bashrc_x file did. You may then source bashrc_a, do your work, source unbashrc_a, followed by source bashrc_b (or have bashrc_b source unbashrc_a directly).

This may be tricky to write and to keep up to date though.

  • Not exactly what I wanted, but I kinda figured a way with your answer. It turns out, to make bash forget all environment arguments, just bash --login is enough. So I -similar to your answer- removed bashrc_a from default .bashrc and made a couple of scripts to switch between two environments without a big effort. – Hojin Cho Sep 19 '17 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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