47

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?

3
  • Environmental variables that are also used by non-interactive shells (say, a shell script you wrote) should go into .zshenv.
    – Francisco
    May 27, 2013 at 8:20
  • Is ~/.zshenv sourced by login shells as well? Should PATH be defined there?
    – jordelver
    May 27, 2013 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, 2013 at 7:51

2 Answers 2

32

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.

3
  • That's great, thanks. I'll use export as a general rule now then.
    – jordelver
    May 24, 2013 at 11:13
  • 6
    But how do the two differ? Especially in the case where it is not about PATH. What if I do JAVA_HOME=... versus export JAVA_HOME=.. Feb 14, 2020 at 14:45
  • 2
    you probably want export” (emphasis mine) doesn’t actually the question of how they differ, and it only probably answers whether OP should include export. More, the answer does give good advice about not erasing PATH, but that advice is not related to the question.
    – Lucas
    Dec 25, 2020 at 19:20
29

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/bin

Both variants are automatically kept in sync. So, what's the benefit of using an 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
path=(/new/path1
      /new/path2
      $path)
export PATH
4
  • Thank you, that was actually a follow up question. I will look at managing my path using path rather than PATH.
    – jordelver
    May 24, 2013 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, 2013 at 11:48
  • If I add an entry to Zsh’s path, it’s not also added to PATH, is it?
    – Lucas
    Dec 25, 2020 at 19:24
  • 2
    @Lucas: "Both variants are automatically kept in sync."
    – mpy
    Dec 26, 2020 at 9:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .