When setting variables in my ~/.zshrc I can either use export

export PATH=/some/path

or not


How do these differ and which should I use?

  • Environmental variables that are also used by non-interactive shells (say, a shell script you wrote) should go into .zshenv. – Francisco May 27 '13 at 8:20
  • Is ~/.zshenv sourced by login shells as well? Should PATH be defined there? – jordelver May 27 '13 at 21:58
  • 1
    PATH is probably the best example of a variable that should be defined inside ~/.zshenv, this file gets sourced by any zsh session (unless you use some option to turn that off). See man zsh for a review of which files get sourced and in which order. – Francisco May 29 '13 at 7:51
  • Ok, thanks @Francisco I will read the man page. – jordelver May 29 '13 at 12:21

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.

  • That's great, thanks. I'll use export as a general rule now then. – jordelver May 24 '13 at 11:13

Demure already answered your specific question. However this is a zsh question and about PATH. So here is another point: Beside the standard variable $PATH, there is also $path, which is an array. Here you see the difference (colons or not...):

$ print $PATH
$print $path
/bin /usr/bin /usr/local/bin /usr/X11R6/bin

Both variants are automatically kept in sync. So, what's the benefit of using a array?

  • The latter you can declare via typeset -U path to "keep only the first occurrence of each duplicated value" (from man 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. https://stackoverflow.com/q/3435355/2037712 or http://www.zsh.org/mla/users//2005/msg01132.html
  • 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
export PATH
  • Thank you, that was actually a follow up question. I will look at managing my path using path rather than PATH. – jordelver May 24 '13 at 11:03
  • 1
    Glad to hear as I already feared that I'm OT. Btw. another advantage I forgot: You can easily loop over the elements with for i ($path) { print $i # or do something else }. – mpy May 24 '13 at 11:48

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.