0

I have portable terminal with bash shell on usb drive, and I want to load different environment variables every time I change computer, users are different on other machines. How to setup .bashrc file for this?

1

I am not certain I completely understand what you want to do.

I suspect that what you will basically want is to keep a few environment files you can source when/where needed.

Assuming you wanted to have your environment set on a remote machine you ssh into, then your .bashrc could contain something like

source ~/path/to/file.env

or

. ~/path/to/file.env

Do keep in mind that .bashrc may not be the file you do need to edit. From my system's bashrc:

# There are 3 different types of shells in bash: the login shell, normal shell
# and interactive shell. Login shells read ~/.profile and interactive shells
# read ~/.bashrc; in our setup, /etc/profile sources ~/.bashrc - thus all
# settings made here will also take effect in a login shell.

If you are switching users locally, this approach would also work.

If what you wanted was to modify your own environment (not sure why you would, but not here to judge), then probably the easiest is to just manually source whatever env file you need as and when you need it.

It sort of sounds like you are on Windows, though, in which case you might need to make sure your local shell works similar to bash, and allows sourcing things.

Also, do keep in mind you can use the env command to run things, as in:

env -i bash -c 'source .foo.env && do_something.sh'
  • I think about some script, if I am on pc1 use env1 if pc2 use en2. Yes, I am on Windows, running MobaXterm with bash – dukasvili Feb 28 '17 at 9:40
  • Ok - the absolute easiest way is to just manually load an env file when you need it. I guess, if you really wanted to, you could have some script do this for you, for example by looking at your hostname and/or user name and source the 'correct' env file. Something like source ~/envs/$HOSTNAME_$USER.env in your .bashrc should work, – iwaseatenbyagrue Feb 28 '17 at 9:43
  • Yes that is exactly what i want. – dukasvili Feb 28 '17 at 9:54

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.