How to add global env bash variables to your mac os x terminal app

Posted on November 27, 2021

TIL how to add global enviroment variables, those that can be used with the Terminal Shell.

First, open this file:

open ~/.bash_profile

You should have the file open with your default text editor.

Then, add the variable, such as:

export MY_VAR="Some Content"

Save it, close the Terminal window, open another one and type:

echo $MY_VAR

You should see "Some Content" printed out.

I've learned the details here but I've discovered that on MacOsX the default profile that is loaded on each Terminal window is ~/.bash_profile and not ~/.bashrc neither ~/.profile, therefore, use the first one to have variables persisting in your Terminal shell.

When Terminal.app opens a new window, it will run .bash_profile. Not, as users familiar with other Unix systems would expect, .bashrc.

Last, use this command to load immediatly the new variables in the same open window:

source ~/.bash_profile

That's all.