How to modify the PATH environment variables in Catalina?

10,530

Solution 1

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.

Solution 2

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

Solution 3

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.

Share:
10,530

Related videos on Youtube

Niing
Author by

Niing

Updated on September 18, 2022

Comments

  • Niing
    Niing over 1 year

    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