Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

export PATH=/some/path

or not

PATH=/some/path

How do these differ and which should I use?

share|improve this question
    
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
    
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

2 Answers 2

up vote 6 down vote accepted

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.

share|improve this answer
    
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
/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin
$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. http://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
path=(/new/path1
      /new/path2
      $path)
export PATH
share|improve this answer
    
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
    
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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