4

Some application tells me to add something to the PATH environment variables, I know one way to do this is by creating a file at ~/.zshrc, but I would like to know where is the file the variable PATH=... is stored?

When I typed echo $PATH I got:

/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/share/dotnet:~/.dotnet/tools:/Library/Apple/usr/bin:/Library/Frameworks/Mono.framework/Versions/Current/Commands:/Applications/Xamarin Workbooks.app/Contents/SharedSupport/path-bin

From the file /etc/paths I got:

/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin

So which file appended the additional paths?


Reply to the comment:

I got: (forgive me that I changed my user name to alice)

grep: /Users/alice/.profile: No such file or directory
grep: /etc/environment: No such file or directory
grep: /etc/zsh/zprofile: No such file or directory
grep: /Users/alice/.zshrc: No such file or directory
grep: /Users/alice/.profile: No such file or directory
4
  • Please edit your question and show us the output of grep dotnet ~/.profile /etc/environment /etc/profile /etc/zsh/zprofile ~/.zshrc ~/.profile. – terdon Apr 16 '20 at 8:49
  • 1
    Argh, crap. Looks like mac moves some files around. I would also try grep -R dotnet /etc 2>/dev/null and grep dotnet ~/.* 2>/dev/null. Simply searching for dotnet, one of the extra things in your $PATH, in all files in /etc and any dotfiles in your home dir. – terdon Apr 16 '20 at 9:04
  • Great! Could you please post an answer explaining what you found? – terdon Apr 16 '20 at 9:21
  • Of course. Whenever you can! – terdon Apr 16 '20 at 9:35
4

As you said in your own self-answer, you don't need to know where the PATH variable is originally populated.

You may easily add new paths to the PATH at either end or beginning, and zsh also has a convenient way of removing duplicates.

You may make the PATH variable, and the associated path array, unique first, by using

typeset -U -g PATH path

The variables will then stay unique by virtue of this command.

To prepend a couple of paths:

path=( /new/path1 /new/path2 $path )

To append a couple of paths:

path=( $path /new/path1 /new/path2 )

or,

path+=( /new/path1 /new/path2 )

The PATH variable's value will be updated accordingly.

You may want to do this in your $ZDOTDIR/.zprofile file, which is sourced automatically by any zsh login shell. Doing it in your $ZDOTDIR/.zshenv file would be unnecessary as that file is sourced by any type of zsh invocation (and should therefore be kept really short, if it's needed at all; I just set ZDOTDIR=$HOME/.zsh in there, for example).

Note that PATH is already an environment variable, so exporting it again serves no purpose.

3
  • About ZDOTDIR=$HOME/.zsh, if I set this on /etc/zshenv, would it be a good idea? (I originally set it on ~/.zshenv, but this way I will have both ~/.zshenv and ~/.zsh/, but I want ~/.zshenv also in ~/.zsh/ folder) – Niing Apr 20 '20 at 8:00
  • 1
    @Niing If /etc/zshenv sets ZDOTDIR to $HOME/.zsh, then ~/.zsh/.zshenv will be read rather than ~/.zshenv (for all users). Personally I have ~/.zshenv as a symbolic link to ~/.zsh/.zshenv. – Kusalananda Apr 20 '20 at 8:05
  • Thank you so much! I should adopt your method. – Niing Apr 20 '20 at 8:16
1

The file /etc/zprofile uses /usr/libexec/path_helper to set the default PATH and we don't have the source code of it.

The following shows you how to modify user-wise PATH:

Create/edit ~/.zprofile and add the following content:

# append to PATH
export PATH="$PATH:.../to/target/bin"

# prepend to PATH
export PATH=".../to/target/bin:$PATH"

# remove duplicate in PATH
typeset -U PATH
1

The following steps show you how to modify system-wise PATH:

  1. Create a file with the naming convention: priority-appname(e.g. '20200418-vscode', I just use date to make the priority unique) Then store the path you want to add to PATH in it.

  2. Move that file to /etc/paths.d/, done.

Notice1: Remember to create a new terminal session to see the change in echo $PATH.

Notice2: this method only appends your path to PATH.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.