I have a .zshrc and a .zshenv in my home directory. I added a new command to my .zshenv that I discovered was being run at least 3 times. I realized that I was accidentally sourceing it from the .zshrc, so I removed that line, but it's still being run twice.

I've tried echoing $0 and $_ in the script, and I get (~/.zshenv) (~/.zshenv) for the first pass, and (30) (zsh) for the second. What could that mean? Is there a better way to get a "stack trace" showing my caller?

  • 30 is probably a line number. – slm Aug 27 '13 at 9:04

When you invoke zsh you can debug what's going on by using the -x switch. It's similar to bash's -x switch, where it shows each line as it's executed along with any results.

$ zsh -x
+/etc/profile.d/mc.sh:3> alias 'mc=. /usr/libexec/mc/mc-wrapper.sh'
+_src_etc_profile_d:9> i=/etc/profile.d/PackageKit.sh
+_src_etc_profile_d:10> [ -r /etc/profile.d/PackageKit.sh ']'
+_src_etc_profile_d:11> . /etc/profile.d/PackageKit.sh
+_src_etc_profile_d:9> i=/etc/profile.d/qt.sh
+_src_etc_profile_d:10> [ -r /etc/profile.d/qt.sh ']'
+_src_etc_profile_d:11> . /etc/profile.d/qt.sh
+/etc/profile.d/qt.sh:9> [ -z /usr/lib64/qt-3.3 ']'
+_src_etc_profile_d:9> i=/etc/profile.d/udisks-bash-completion.sh
+_src_etc_profile_d:10> [ -r /etc/profile.d/udisks-bash-completion.sh ']'
+_src_etc_profile_d:11> . /etc/profile.d/udisks-bash-completion.sh
+/etc/profile.d/udisks-bash-completion.sh:3> [ -z '' ']'
+/etc/profile.d/udisks-bash-completion.sh:3> return
+_src_etc_profile_d:9> i=/etc/profile.d/vim.sh
+_src_etc_profile_d:10> [ -r /etc/profile.d/vim.sh ']'
+_src_etc_profile_d:11> . /etc/profile.d/vim.sh
+/etc/profile.d/vim.sh:1> [ -n '' -o -n '' -o -n 4.3.10 ']'

The output can also be redirected to a file for later review.

$ zsh -x 2>&1 | tee zsh.log

This will appear to hang at the end, just Ctrl + c to stop it, and then check out the resulting log file, zsh.log.

| improve this answer | |
  • Apparently, in this case, /etc/zshenv was sourceing ~/.zshenv, but (as far as I can tell) I think zsh will source that file as part of its startup. The trace showed the source invocation from /etc/zshenv, then just started right back in on ~/.zshenv a second time! – Coderer Aug 27 '13 at 13:39
  • @Coderer - yes a fair amount of files get sourced automagically thanks to /etc and /etc/profile.d on some systems. – slm Aug 27 '13 at 13:52

Have you applied changes with source ~/.zshrc? Or logged out and back in for the changes to take effect.

| improve this answer | |
  • 2
    This sounds more like a comment than an answer. – Joseph R. Aug 27 '13 at 9:40

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.