1

According to the docs

.zshenv is sourced on all invocations of the shell, unless the -f option is set. It should contain commands to set the command search path, plus other important environment variables. .zshenv should not contain commands that produce output or assume the shell is attached to a tty.

Okay, cool, so I would assume that logging in via ssh is considered an invocation of zsh... but maybe not?

wayne@arglefraster ~ 
⚘ echo $PATH                                                                                                   10:01:17
/usr/local/heroku/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl
wayne@arglefraster ~ 
⚘ zsh                                                                                                          10:01:20
e%                                                                                                                      
wayne@arglefraster ~ 
⚘ echo $PATH                                                                                                   10:01:24
/usr/local/heroku/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl:/home/wayne/.rvm/bin:/home/wayne/.bin:/home/wayne/.local/bin

Those extra bits of path are added in my .zshenv file.

Is this normal, or do I have something wonky going on? Should I move my PATH modifications elsewhere?

Wayne Werner
  • 11,713
  • You have zsh as your default shell in /etc/passd or you start it somehow different? – Jakuje Mar 25 '16 at 15:35
  • @Jakuje almost added this to the question: wayne:x:1000:100::/home/wayne:/bin/zsh – Wayne Werner Mar 25 '16 at 15:37
  • Use zsh -o SOURCE_TRACE to confirm what files are being read, and then zsh -x to see what configuration is done, in particular for PATH, in the event something else is trampling the .zshenv changes. – thrig Mar 25 '16 at 15:57
  • See this answer, which is probably what you want. mark it as chosen if it is. https://unix.stackexchange.com/a/752811/3285 – Evan Carroll Jul 30 '23 at 18:50

2 Answers2

6

zshenv is indeed read by every instance of the shell, but it is read first, before anything else. Your $PATH is probably getting reset in zprofile (~/.zprofile or /etc/zprofile, ...).

zshenv is not a good place to set your $PATH. It belongs in zprofile, which has the job of setting up the environment upon login. Setting environment variables in zshenv means among other things that they will get clobbered when running simple zsh subshells or zsh scripts within one session.

Celada
  • 44,132
  • Well, it definitely wasn't getting reset in ~/.zprofile or /etc/zprofile, as I have neither of those files. Nor ~/.zshrc, as there's no PATH variable there. But, setting the PATH in ~/.zprofile does appear to work properly. Following @thrig's comment, it may have been the virtualenvwrapper, though it seems strange that it would totally clobber the PATH – Wayne Werner Mar 28 '16 at 02:48
  • 5
    I'm confused. This is the opposite of this answer here which says ~/.zhsenv should have the $PATH . – Leonard Jan 17 '21 at 17:17
0

Confirm you're in Z shell and that's your login shell

.zshenv is supposed to run when you ssh into a machine. If it doesn't run,

  • Check that you're in Z shell when you ssh into a machine with echo $SHELL
  • If you're not in Z shell, run zsh which will explicitly start Z shell and confirm your .zshenv works. If it does, run exit to drop back out of Z shell.
  • If the file works, change your login shell,
    • While logged in via ssh
    • Run command -v zsh so you know where Z shell is, if this returns nothing you need to install Z shell
    • then run chsh and provide it the path to Z shell
    • Log out and log back in with ssh.
Evan Carroll
  • 30,763
  • 48
  • 183
  • 315