When setting variables in my ~/.zshrc I can either use export
export PATH=/some/pathor not
PATH=/some/pathHow do these differ and which should I use?
32 Answers
If you want programs run from zsh to see the var, export it.
For path, you probably want to export.
Instead of export PATH=/some/path you probably want export PATH="$PATH:/some/path", unless you intend to clear out the system preset path completely.
Demure already answered your specific question. However, this is a zsh question and about PATH. So here is another point: besides the standard variable $PATH, there is also $path, which is an array. Here you see the difference (colons or not...):
$ print $PATH
/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin
$ print $path
/bin /usr/bin /usr/local/bin /usr/X11R6/binBoth variants are automatically kept in sync. So, what's the benefit of using an array?
- The latter you can declare via
typeset -U pathto "keep only the first occurrence of each duplicated value" (fromman zshbuiltins). That means this keeps your path clean, even if you successively source your~/.zshrc(because you changed it or whatever) and do not clutter it up with the same values again and again. - You can use
path+=(/new/path)to add a new directory to your PATH. To remove an element you have to use some tricks, see e.g. or - You can easily loop over the elements in the PATH via
for i ($path) { print $i # or do something else }
Finally, here is an excerpt from my config:
typeset -U path
path=(/new/path1 /new/path2 $path)
export PATH 4