Search code examples
zshoh-my-zshpath-variables

How can I change PATH variable in zsh?


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.

Solution

  • 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