Coder Perfect

In ZSH, adding a new entry to the PATH variable.


I’m using zsh and I’m trying to add a new entry to the PATH variable (/home/david/pear/bin), but I’m not sure how.

The thing that confuses me the most is that there’s not a single reference to a PATH variable in my ~/.zshrc file, but doing echo $PATH returns:


So, somehow, a PATH variable is set.

Asked by David Barreto

Solution #1

In fact, ZSH allows you to employ unique mappings of environment variables. So here’s what you can do:

# append
# or prepend
path=('/home/david/pear/bin' $path)
# export to sub-processes (make it inherited by child processes)
export PATH

That, in my opinion, is a really useful feature that may be applied to other variables. Example:

typeset -T LD_LIBRARY_PATH ld_library_path :

Answered by ony

Solution #2

Add the following line to.zshrc:

export PATH=/home/david/pear/bin:$PATH

EDIT: While this works, ony’s response is preferable because it makes use of the structured interface ZSH provides for variables like $PATH. This is a conventional bash technique, but as far as I’m aware, there’s no reason to use it when ZSH has better choices.

Answered by Linuxios

Solution #3

You can add to your PATH in a simple manner. Unless you’re appending multiple elements, there’s no need for parenthesis. It also frequently does not necessitate the use of quotation marks. So here’s a quick and easy way to append:


This lower-case syntax treats path as an array, but it also has an impact on its upper-case counterpart, PATH (to which it is “bound” via typeset).

(Notice that the separator no: is required/desired.)

Then the standard pattern for evaluating a new script/executable is as follows:

# or

You can scatter path settings throughout your.zshrc (as seen above), and the earlier listed settings will take precedence (though you may wish to use the “prepend” form path=(/some/new/bin/dir $path) on occasion).

Treating path in this manner (as an array) also eliminates the requirement for a rehash to locate newly pathed instructions.

Also take a look at vared path as a dynamic way to edit path (and other things).

For this query, you may simply be interested in path, but since we’re talking about exports and arrays, keep in mind that arrays cannot be exported in general.

You can even prevent duplicate entries from being added to PATH (see this and this):

typeset -U path

Your system shell files set your path for you, which is why it already has some entries in it. This has been discussed in a couple of previous posts:

Answered by Micah Elliott

Solution #4

a single liner with no opening file /.zshrc

echo -n 'export PATH=~/bin:$PATH' >> ~/.zshrc


echo -n 'export PATH=$HOME/bin:$PATH' >> ~/.zshrc

Do source /.zshrc in the same tab or a new tab to see the result.

Answered by Siva Praveen

Solution #5

Answered by saneryee

Post is based on