How to set environment variables on Mac OS and Ubuntu?

Setting an environment variable is a developer thing. When we are building applications it requires setting environment variables. Because some of the things are specific to the environment. How do we set the environment variable in Mac or Ubuntu?

We can set the environment variable using the .bash_profile the file of the user’s home directory. In many Mac OS versions, this file may not exists we will have to create one in that case.

Open the .bash_profile in terminal like this:

$ vi .bash_profile

Let say we want to set Java home as an environment variable. We need to add the following line in the .bash_profile file.

export JAVA_HOME=`/usr/libexec/java_home -v 1.8`

Now we need to source the file using the following command:

$ source .bash_profile

Now from the command line we can check by using the following command:

$ echo $JAVA_HOME

We can also verify using the env command:

$ evn
Reference

https://stackoverflow.com/questions/7501678/set-environment-variables-on-mac-os-x-lion