The Computer Oracle

zsh config - to export or not to export?

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Forest of Spells Looping

--

Chapters
00:00 Zsh Config - To Export Or Not To Export?
00:20 Accepted Answer Score 34
00:42 Answer 2 Score 30
01:49 Thank you

--

Full question
https://superuser.com/questions/598810/z...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#shell #zsh

#avk47



ACCEPTED ANSWER

Score 34


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.




ANSWER 2

Score 30


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