How to set environment variable permanently on macOS Catalina?

I am trying to set two environment variables on macOS version 10.15.5, I've opened a terminal and tried:

vi ~/.bash_profile

Added the exports to that, saved and exited vi, closed and reopened terminal, I don't see my new exports.

I've also tried:

vi ~/.profile

Added the exports to that, saved and exited vi, closed and reopened terminal, I don't see the exports there either.

What haven't I done?


Sorted, vi ~/.zshenv.

Added the exports, save and exit, restart terminal, my system was using the zsh terminal.

Or without restarting the terminal, just add:

source ~/.zshenv or source ~/.profile

After editing.


Starting with macOS Catalina -- by default it takes zsh and hence you need to change in zprofile. Please follow below steps

  1. Launch Terminal app

  2. Create profile file:

    touch ~/.zprofile
    
  3. Open file in editor:

    open ~/.zprofile
    
  4. Add any exports definitions you need, one by line.

    For example:

    export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-11.0.11.jdk/Contents/Home
    export GRADLE_HOME=/Users/myuser/Documents/installs/gradle-6.8.3
    export PATH=$PATH:/Users/myuser/Documents/installs/gradle-6.8.3/bin
    
  5. Execute new .zprofile by either restarting Terminal or using this:

    source ~/.zprofile