Search code examples
macospathzshenvironment

Why do my $PATH environment always reset after I open a new terminal on my mac?


Usually I will nano .zsh_profile Then I will edit the path

#PYTHON
export PATH=/Users/ffff/Library/Python/3.8/bin:$PATH

# JAVA
export JAVA_HOME=$(/usr/libexec/java_home)
export PATH=$JAVA_HOME/bin:$PATH

#ANDROID
export ANDROID_HOME=/Users/ffff/Library/Android/sdk
export PATH=$ANDROID_HOME/platform-tools:$PATH
export PATH=$ANDROID_HOME/tools:$PATH
export PATH=$ANDROID_HOME/tools/bin:$PATH

then I will save and exit Then I will

source .zsh_profile

I will test the Java and ADB all is good, but one I open a new Terminal from my mac, it will say ADB and JAVA and Android_HOME not found

Why is it not persist? Did I miss out anything? My mac version is 12.2.1


Solution

  • The .zsh_profile file, has no special meaning. This is probably a confusion form Bash, where the bash_profile is executes for login shells. In Zsh, that equivalent would be ~/.zprofile. You might have some code in your .zshrc file, that overrides the PATH variable with something else. Because, as oppsed to .zprofile, the .zshrc file gets executed for every interactive non-login shells.

    So my advice, checkout .zshrc and see if there's something overriding the PATH there, if so, maybe you want to change that, and NOT to execute these commands you want to add for every time a shell is opened, you should put them in .zprofile, so they only get executed once at login.