How to change environment variable when user change?

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?

dukasvili

Posted 2017-02-28T09:07:17.283

Reputation: 373

Answers

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'

iwaseatenbyagrue

Posted 2017-02-28T09:07:17.283

Reputation: 216

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 – 2017-02-28T09:40:51.260

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 – 2017-02-28T09:43:50.933

Yes that is exactly what i want. – dukasvili – 2017-02-28T09:54:38.060