Zsh, the popular Unix shell, utilizes a series of startup files to configure the shell environment upon launch. Two of these files, .zshenv and .zshrc, play crucial roles in setting up the shell environment for different use cases. Understanding the distinction between these two files is essential for effective Zsh customization.

What is Zshenv?

.zshenv stands for “ZSH environment file”. It is sourced by Zsh for every invocation, regardless of whether the shell is interactive or non-interactive. This makes it ideal for setting environment variables that should be available in all Zsh sessions.

What is Zshrc?

.zshrc stands for “ZSH resource file”. It is sourced by Zsh for interactive shells only. This means it is not loaded when Zsh is invoked as a non-interactive script or when a program launches Zsh. Consequently, .zshrc is typically used to set up customizations and aliases that are specific to interactive shell sessions.

Key Differences Between Zshenv and Zshrc

FeatureZshenvZshrc
LoadingSourced for every Zsh invocation, regardless of interactivitySourced for interactive Zsh shells only
PurposeSetting environment variables that should be available in all Zsh sessionsSetting up customizations and aliases specific to interactive shell sessions
Examples of UseSetting PATH, EDITOR, PAGERDefining aliases, functions, and prompt customizations

When to Use Zshenv vs. Zshrc

  • Use .zshenv for:

    • Setting environment variables that should be available in all Zsh sessions, such as PATH, EDITOR, and PAGER.
    • Setting variables that need to be available to scripts and non-interactive Zsh sessions.
  • Use .zshrc for:

    • Defining aliases, functions, and prompt customizations that are specific to interactive Zsh shell sessions.
    • Loading additional configuration files, such as those for plugins or themes.

Conclusion

Zshenv and Zshrc play distinct roles in Zsh’s startup process. Zshenv is used for setting environment variables that should be available in all Zsh sessions, while Zshrc is used for setting up customizations and aliases specific to interactive shell sessions. By understanding the differences between these two files, you can effectively customize your Zsh environment to suit your needs.

Additional Resources