Skip to main content

Environments

Whether or not you realize it, your current session of your shell has a bunch of variables set. Most are just set by the OS, some by Multi-pass (or whatever you're using to run your computer), some by various programs, and some by you.

Go ahead and run blow command into terminal to see what variable have been set.

printenv

You'll probably see a super long list of environmental variables. Various programs will refer to these to modify how they work. And what's great you can modify these, either permanently, just for this session, or just for one command.

Per Session

So what if we want to modify a variable for a whole session (until you close this open session of bash; if you open another tab of bash, even while this one is still running, it is a different session):

echo $GREETING # nothing
GREETING=hello
echo $GREETING # hello

Again, once we close this window, GREETING goes away.

Permanent

So what if we want to last forever? There are a few options but really only one is recommend.
The first is editing;

sudo vim /etc/environment

This will modify every user's environment so it's often not what you want. Each line in that environment file's format should be VARIABLE=value with one per line.

Similar with /etc/profile and /etc/bashrc except with these you can actually invoke scripts within them. Again, this is system-wide and not usually what you want.

.bashrc and .bash_profile

In your home directory, there are two files;

  1. .bashrc
  2. .bash_profile

These are the files you need to configure and customize your bash shell. You can set things like telling Node.js you're in development mode, set up git how you want to, customize colors, set path, or really anything you can write a bash command for.

.bash_profile

bash_profile is only run on login shells. That is to say, it's only run once for each time you log in to your computer. It is not run after that.
Actually, what I'd suggest you do is go put this in your .bash_profile:

sudo vim .bash_profile
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi

That way your .bashrc is always run. And after you put this in there you can just forget .bash_profile exists and always just modify .bashrc.

.bashrc

.bashrc is run on every non-login shell, so it's run on every tab of bash you start up. Typically what you want is to run your customizations on every shell so you actually just want to modify .bashrc.
If you not adding the script line in the .bash_profile then you can run this command to edit the .bashrc .

sudo vim ~/.bashrc

for quite the vim editor press esc and type :wq if edit file, if not then just use :q and press enter. Okay, so now to have variables that affect all shells, you just put a line in there that says:

export VARIABLE=value

and now it will survive when you log out.
Just FYI, if you want that variable to affect this shell, you'll have to do a . ~/.bashrc so that it will reload your .bashrc. The . means execute in this context. You also could say source ~/.bashrc and that would work too.