How to set up environment variables on macOS

Environment variables are system wide values that applications can use to retrieve information specific to your local configuration. This ability is useful for storing credentials or file paths that need to be shared amongst multiple programs. On the Mac, environment variables are stored in the bash profile located in the root level of the home directory:


Note: If the file doesn’t exist yet, it can be created.

Open the file with your favorite text editor:

nano .bash_profile

My very first use case was for bash_profile was to store FTP information. Saving that information to bash_profile allows the repo code to be uploaded without revealing sensitive information. To do so, the following lines can be added:

#FTP Credentials
export FTP_USERNAME=myusername
export FTP_PASSWORD=mypassword

Save/Write Out with Control+O (press return to confirm the name if the file is being generated for the first time). Then exit with Control+X.

Pro Tip: Once information is added to the bash_profile, your current shell session will not register the new information automatically. Save yourself unnecessary frustration by closing and reopening your terminal window. Alternatively you can jump start it with the following command:

source ~/.bash_profile

With the information now saved globally into the system, you can access it from any script by using the following structure:


here it is put into context:

var user = process.env.FTP_USERNAME;

Add a Comment

Your email address will not be published. Required fields are marked *