Adding a new entry to the PATH variable in ZSH Adding a new entry to the PATH variable in ZSH linux linux

Adding a new entry to the PATH variable in ZSH


Actually, using ZSH allows you to use special mapping of environment variables. So you can simply do:

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

For me that's a very neat feature which can be propagated to other variables.Example:

typeset -T LD_LIBRARY_PATH ld_library_path :


Here, add this line to .zshrc:

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

EDIT: This does work, but ony's answer below is better, as it takes advantage of the structured interface ZSH provides for variables like $PATH. This approach is standard for bash, but as far as I know, there is no reason to use it when ZSH provides better alternatives.


You can append to your PATH in a minimal fashion. No need forparentheses unless you're appending more than one element. It alsousually doesn't need quotes. So the simple, short way to append is:

path+=/some/new/bin/dir

This lower-case syntax is using path as an array, yet alsoaffects its upper-case partner equivalent, PATH (to which it is"bound" via typeset).

(Notice that no : is needed/wanted as a separator.)

Common interactive usage

Then the common pattern for testing a new script/executable becomes:

path+=$PWD/.# orpath+=$PWD/bin

Common config usage

You can sprinkle path settings around your .zshrc (as above) and it will naturally lead to the earlier listed settings taking precedence (though you may occasionally still want to use the "prepend" form path=(/some/new/bin/dir $path)).

Related tidbits

Treating path this way (as an array) also means: no need to do arehash to get the newly pathed commands to be found.

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

You may only be interested in path for this question, but sincewe're talking about exports and arrays, note thatarrays generally cannot be exported.

You can even prevent PATH from taking on duplicate entries(refer tothisand this):

typeset -U path