Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

How to Set Environment Variables in Zsh

Use export for the current shell, choose the right Zsh startup file, and verify that a child process receives the variable.

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

How to set environment variables in ZSH Picture 1

How to set environment variables in ZSH Picture 2

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 MTE

Use 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

  • ~/.zshrc runs for interactive Zsh sessions. Use it for terminal settings that should be available when you type commands.
  • ~/.zprofile runs for login shells. Use it for settings you want initialized at login.
  • ~/.zshenv runs 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"

How to set environment variables in ZSH Picture 3

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.

How to set environment variables in ZSH Picture 4

How to set environment variables in ZSH Picture 5

How to set environment variables in ZSH Picture 6

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.

How to set environment variables in ZSH Picture 7

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.