Thursday, June 7, 2018

How to set environment variables on MacOS?

There are two ways to set environment variables on MacOS (and I assumed is the same in linux):

1. Temporary
The easy and fast way to set an environment variable, and is perfect that tasks that required one of those.
export [VARIABLE_NAME]=[PATH]
Example:
export JAVA_HOME=/usr/libexec/java_home
Another example is when is required add a path to the PATH variable, without lost the current content:
export PATH=/THE/NEW/FOLDER/:$PATH
Unfortunately, is our terminal is closed, we lost the variables previously settled.

2. Permanently
If we need to keep the variables in a permanent way, we need not only use export command, but using on a file called .bash_profile. This file must be on the user folder, and normally it doesn't exist, so, we need to create it with our favorite editor, or using this command:
touch ~/.bash_profile
Inside this file we need to put all the path using export that we want:
export JAVA_HOME=/usr/libexec/java_home 
export PATH=/Users/username/Library/Android/sdk/platform-tools:$PATH
This file looks like:

That's all.