[macOS] How to set environment variable on macOS

It is very common for developers to set Environment Variable while doing some development work or configuring the development environment.

There are two kinds of Environment Variable: temporary and permanent. This post will show how to set both on macOS

1. Temporary environment variable

We can simply execute the below command in the Terminal

export [env_key]=[env_value]

For example, to set DEBUG_MODE to true before running the development project.

export DEBUG_MODE=true

2. Permanent environment variable

  1. Go to the user working directory to locate the file .bash_profile. Normally we can use vi ~/.bash_profile.
  2. Add the below command to the bottom of the file
export [env_key]=[env_value]
  1. Save the changes with :wq in vim
  2. Refresh the cache of .bash_profile with source ~/.bash_profile

After setting the environment variable, we can type echo ${env_key} to validate if it is successfully set.


If this post helped you to solve a problem or provided you with new insights, please upvote it and share your experience in the comments below. Your comments can help others who may be facing similar challenges. Thank you!
Buy Me A Coffee
DigitalOcean Referral Badge
Sign up to get $200, 60-day account credit !