Table of Contents
Use export NAME=value to set an environment variable for the current Zsh session and the programs it launches. To set that value again in future interactive sessions, add the export to ~/.zshrc.
Set a variable for the current session
export MTE=7
printenv MTE
Use no spaces around the equals sign. Quote values that contain spaces. A regular assignment such as MTE=7 creates a shell variable. Exporting passes it to commands started afterward. To set a value for just one command, use an assignment prefix:
MTE=7 printenv MTEUse unset MTE to remove it from the current shell. A colon only acts as a separator for variables that expect one, such as PATH.
Choose the right startup file
~/.zshrcruns for interactive Zsh sessions. Use it for terminal settings that should be available when you type commands.~/.zprofileruns for login shells. Use it for settings you want initialized at login.~/.zshenvruns much more broadly, including for non-interactive scripts. Keep it minimal.
These paths assume the default configuration directory. See the Zsh startup-file documentation for the order and the effect of ZDOTDIR.
Save and verify a persistent setting
Open ~/.zshrc in a text editor and add a line such as:
export PROJECT_LABEL="Demo Project"
Open a fresh interactive Zsh or run source ~/.zshrc to re-run the startup file in the current shell. Use printenv PROJECT_LABEL to check that the variable was exported.
printenv with no name shows all exported variables. Avoid sharing its output because it can contain credentials. To troubleshoot executable paths, see how to fix a missing command in Zsh.
Reader Comments 0
Sign in with email or Google to join the discussion.