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?
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.
Sign up to join this communityIf 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.
PATH
. What if I do JAVA_HOME=...
versus export JAVA_HOME=..
Feb 14, 2020 at 14:45
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.
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?
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.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.htmlfor 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
path
rather than PATH
.
May 24, 2013 at 11:03
for i ($path) { print $i # or do something else }
.
.zshenv
.~/.zshenv
sourced by login shells as well? ShouldPATH
be defined there?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). Seeman zsh
for a review of which files get sourced and in which order.