~/.zshenv runs first on all shell sessions (login/non-login, interactive/non-interactive) for universal environment variables like PATH; ~/.zprofile runs for login shells only (like a fresh terminal window) for login-specific settings (PATH, terminal title); and ~/.zshrc runs for interactive shells (every time you open a new terminal/subshell) for aliases, functions, prompts, and other interactive customizations. Use ~/.zshenv for global envs, ~/.zprofile for login setups, and ~/.zshrc for daily interactive tweaks.
- When: Every single time a Zsh shell starts (login, non-login, interactive, non-interactive).
- What: Essential, fundamental environment variables that must be set for any shell invocation, like
$PATH. - Don't put: Commands that produce output or rely on an interactive terminal (tty).
- When: Only for login shells (e.g., first terminal window, SSH login).
- What: Commands that should run once per login session, like setting the terminal title or loading specific modules.
- Why: Variables set here (like
$PATH) are inherited by all subsequent shells, unlike~/.zshrcsettings which only affect interactive sessions.
- When: Every interactive shell (new terminal, new subshell).
- What: Aliases, functions, key bindings, prompt customization (PS1), command completion, and other interactive features.
- Why: It's the primary file for customizing your day-to-day interactive experience.
~/.zshenv(sets global environment variables).~/.zprofile(sets login-specific environment/tasks).~/.zshrc(sets interactive settings, overrides profile settings).~/.zlogin(runs after profile/rc for final login tasks).
*AI responses may include mistakes.
- https://gist.github.com/pbrisbin/45654dc74787c18e858c
- https://zerotohero.dev/tips/zshell-startup-files/
- https://unix.stackexchange.com/questions/71253/what-should-shouldnt-go-in-zshenv-zshrc-zlogin-zprofile-zlogout
- https://www.reddit.com/r/zsh/comments/kwmrf4/help_me_understand_best_practices_re/
- https://zsh.sourceforge.io/Intro/intro_3.html
- https://stackoverflow.com/questions/73416959/why-vs-code-on-macos-suggests-add-path-to-zprofile-instead-of-zshrc
- https://www.reddit.com/r/zsh/comments/1ezfy7k/what_cant_be_dumped_into_zprofile/
- https://community.cisco.com/t5/devnet-general-knowledge-base/developer-basics-creating-aliases-with-bashrc-and-zshrc/ta-p/4672529
- https://mac.install.guide/terminal/zshrc-zprofile
- https://www.freecodecamp.org/news/how-do-zsh-configuration-files-work/