8

I want to change my PATH variable in zsh.

Problem: I don't understand where in the .zshrc file I have to make modifications.

Normally, I would look for the assignment to the PATH variable and set the values from scratch how I would like them to be (leaving all the systems binaries directories untouched).

The first lines in my .zshrc file are as follows:

# If you come from bash you might have to change your $PATH. # export PATH=$HOME/bin:/usr/local/bin:$PATH # Path to your oh-my-zsh installation. export ZSH="/Users/Sam/oh-my-zsh" export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/13/bin 

etc.

My actual PATH variable is:

/Library/Frameworks/Python.framework/Versions/3.9/bin:/Library/Frameworks/Python.framework/Versions/3.8/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/Postgres.app/Contents/Versions/13/bin 

I want to delete the directory where python3.8 is in, it's redundant.

My questions:

  1. Do I have to change line 2 or line 7 in my .zshrc file?
  2. Line 2 is commented out...is it executed anyway at the start of the terminal?
  3. I have tried to comment out line 7. But the postgres directory still remained in my PATH variable which I don't understand.
2
  • This would be better asked on SuperUser or Unix&Linux SE. However I doubt this hasn't already bee answered several times in several flavors there. Commented Jul 31, 2021 at 21:29
  • Did you check PATH in new session? Commented Jul 31, 2021 at 21:33

2 Answers 2

12

The .zshrc is located in the home dir. The dot at the beginning keeps it hidden. Type ls -a from ~ directory to see it. To edit just run vim, nvim, etc as usual.

nvim ~/.zshrc 

This is the command for Neovim. For your editor, sub nvim for the proper command.

Once you get in, you need only add the same export command that you would add from the command line.

export PATH=$PATH:/whatever/you/are/adding 

EDIT

To remove a path variable:

  • First, run the command:
echo $PATH 

from the command line.

  • Next, Copy the output to clipboard.

  • Finally, at the very end of the .zshrc file, add the following:

export PATH=<paste-what-you-copied-here> 

Because you didn't reference $PATH after the =, this will set the path to EXACTLY what you pasted, no more, no less. Adding $PATH: as in the first example will just add whatever to the end of what is already there.

Since this gives you access to every item in the path array, deleting is just a matter of a literal highlight/select and of whatever you want deleted.

Finally, be sure that there is only one place in the file where you are editing PATH. If there are more than one, the result can be confusing to say the least.

That said, I believe the script runs top-to-bottom, so only the last mention should persist. You can take advantage of this in some situations, but for this purpose, one will suffice. XD

Sign up to request clarification or add additional context in comments.

Comments

10

Be careful when you decide to fiddle with the PATH in .zshrc: Since the file is processed by every interactive subshell, the PATH would get longer and longer for each subshell, with the same directory occuring in it several times. This can become a nightmare if you later try to hunt down PATH-related errors.

Since you are using zsh, you can take advantage that the scalar variable PATH is mirrored in the array variable path, and that you can ask zsh to keep entries in arrays unique.

Hence, the first thing I would do is put a

 typeset -aU path 

in your .zshrc; this (due to mirroring) also keeps the entries in PATH unique. You can put this statement anywhere, but I have it for easier maintenance before my first assignment to PATH or path.

It is up to you to decide where exactly you add a new entry to PATH or path. The entries are searched in that order which is listed in the variable. You have to ask yourself two questions:

  • Are some directories located on a network share, where you can sometimes expect access delays (due to bad network conditions)? Those directories should better show up near the end of the path.

  • Do you have commands which occur in more than one directoryin your path? In this case, a path search will always find the first occurance only.

Finally, don't forget that your changes will be seen after zsh processes the file. Therefore, you could create a new subshell after having edited the file, or source .zshrc manually.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.