4

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?

2
  • Which shell are you using?
    – nohillside
    Jul 5, 2020 at 11:12
  • @nohillside, zsh
    – SPlatten
    Jul 5, 2020 at 11:17

3 Answers 3

4

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.

4
  • Each shell is different. But the manual page for the shell typically documents where it finds its startup scripts. E.g. In a terminal window, type man zsh Aug 4, 2020 at 14:42
  • This solution only works for zsh Aug 7, 2020 at 15:14
  • Unless .profile sources .zshenv sourcing this will not help :-)
    – nohillside
    Jan 29, 2021 at 16:40
  • @nohillside, I've done this many times on my own system it works.
    – SPlatten
    Jan 30, 2021 at 6:36
3

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
    
5
  • Why does this differ to the answer given previously apple.stackexchange.com/a/395461/237
    – mmmmmm
    May 24, 2021 at 15:25
  • @mmmmm I have tried to add exact steps which we should follow with examples. Also as it was just mentioned for zsh, sometimes people might get confused with the solution. One more point is if you see comments where its mentioned as this will work only for zsh and hence thought its better to have the detailed steps which people can follow and solve their issues.
    – denzal
    May 24, 2021 at 17:57
  • They are the top of the other answer meeting what the question asked. OK it is an improvement but then it should have been an editr to the other answer. But then again why do you suggest editing a different file?
    – mmmmmm
    May 24, 2021 at 18:02
  • The answer which I have provided (new file) is how I have done it once I got a new mac. This is validated and working fine for all the apps in the mac. If you do the changes in .bash_profile, its not working. I was trying to help but if you feel its not adding any value, let me know. I will delete the answer.
    – denzal
    May 25, 2021 at 5:16
  • The difference is why do you use .zshrc and other answer use .zshenv . Why?
    – mmmmmm
    May 25, 2021 at 7:37
0

answers given above for .zprofile and .zenv can also be applied to .zshrc script file. All three files are invoked on login startup

2
  • 1
    Welcome to Ask Different. This would have been better summited as an edit to the answer, not a separate answer by itself. -- Also, please refrain from using terms like "answers given above" because if there is more than one answer the order of the answers depends on how one has set their preference as Active, Oldest, or Votes. Case in point, there is no answer above this one as it's directly under the question and all the answer are below this one, on my system. Nov 6, 2021 at 15:12
  • point taken, thank you.
    – Leo
    Nov 7, 2021 at 20:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .