I'm trying to get a handle on my shell startup files, and I can't figure out why my etc/zshenv runs twice. I've got echo statements in all of my startup files, and this is what I'm getting:

In /etc/zshenv
In ~/.zshenv for user: davidkennell
In /etc/zshrc
In ~/.zshrc for user: davidkennell
In /etc/zshenv

I have zsh set as my shell via my iTerm2 preferences, telling it in the Profiles/general settings to start the shell by running /usr/local/bin/zsh

  • By any chance do you source /etc/zshenv from your ~/.zshrc? If not, you could temporarily add set -x to /etc/zshenv and then at the end of your ~/.zshrc put set +x, this would print every single command that is executed between the set -x and the set +x, and this might help you pin down why it's getting sourced a second time. You can find some nice flowcharts describing the sequence if you want some more details here
    – einfeyn496
    Jul 10 '20 at 22:01
  • @DavidKennel : Does perhaps one of the files in your startup scripts (such as ~/.zshrc or ~/.zshenv invoke some zsh-script? Since the second execution of /etc/zshenv seems to happen while .zshrc is being processed, I would use set -x to find out exactly, at which point this happens. Jul 14 '20 at 11:06

Given the order of the messages, something in your .zshrc causes /etc/zshenv to be executed. This can be either a call to source /etc/zshenv or an invocation of another instance of zsh. Look for things like

. /etc/zshenv
source /etc/zshenv
zsh -c …
zsh /path/to/file
/path/to/file                             # which starts with #!/usr/bin/env zsh

or variations of these.

Note that the offending line might be either in .zshrc itself or in another file that it sources.

You can run zsh -x or zsh -f -x ~/.zshrc and look for the offending command in the trace. It may be easier than finding it in the source code.

The case of executing another program that happens to be a zsh script is the hardest to detect. For this, it may be easier to monitor access to /etc/zshenv. On the other hand, if that's what's going on, it's perfectly normal and there is nothing to do about it.

  • So I think the offending line in my ~/.zshrc is source $ZSH/oh-my-zsh.sh - commenting it out seems to stop the accessing of /etc/zshenv. However, running zsh -f -x /Users/davidkennell/.oh-my-zsh/oh-my-zsh.sh fills up my entire terminal w/ output, and looking through the source of the file show me that it sources many more oh-my-zsh files. Would love to be able to monitor access out of curiosity, but most tools at the link provided specifically monitor for changes to a file, not when the file is read or sourced. Or, they only work when an app you built it accessing the file. :/ Jul 24 '20 at 0:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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