How to Set Environment Variables in ZSH


Environment variables are dynamic values that configure software and influence program behavior. They provide a convenient way to customize the environment for different apps without modifying the underlying code.

ZSH (Z shell) is a Unix shell designed as an improved alternative to the Bourne shell. With ZSH's robust capabilities and extensive customization options, it is easy to set environment variables to make global changes across all launched programs.

In this tutorial, you will learn to set environment variables using the ZSH shell.

How to set environment variables in ZSH - a tutorial.


  • A system running Linux.
  • Z Shell installed.
  • Access to the terminal (Ctrl + Alt + T).

Note: Check out our guide on how to install Zsh in Ubuntu.

How to Set Environment Variables in ZSH

The easiest way to set environment variables in ZSH is to use the export command. The syntax is:


For example:

export tempvar=15

The command above creates a temporary variable called tempvar and assigns it the value 15. Use the echo command or printenv to verify the variable was created:

Setting a temporary variable in ZSH.

The variable value is saved for the current terminal session. Once you close the terminal, all the environment variables are destroyed. To create permanent environment variables, refer to the next section of the article.

Alternatively, use the set command to create a temporary variable. The syntax is:


When adding multiple values to a variable, separate each value with a colon (:). An example of such an environment variable is the $PATH variable. Use the following syntax:

export VARIABLE_NAME=value1:value2

Similarly, if the variable value contains spaces, enclose the value in quotation marks:

export VARIABLE_NAME="value with spaces"

Environment variables are case-sensitive. The variable names are usually in uppercase to distinguish them in other contexts.

Note: See how to set environment variables in macOS. The default shell in macOS Catalina is ZSH.

How to Create Permanent Environment Variables in ZSH

To make an environment variable persist in ZSH, add it to the .zshrc shell configuration file. If the file does not exist, create it using a text editor. Follow the steps below:

1. Run the following command to create or edit the file with vi:

vi ~/.zshrc

Note: Read our article and learn everything about the .bashrc Bash configuration file.

2. When the file opens, add the environment variable in a new line:


3. Save the file and close the editor:


4. Reinitialize the configuration file to apply the changes to the current session. Run the following command:

source ~/.zshrc

Check if the variable exists by running echo or printenv:

Setting a permanent environment variable in ZSH.

Alternatively, restart the terminal window to end the current session and start a new one with the applied changes.

Test Variable Settings

Check variable settings in ZSH using the echo command or printenv.

  • The syntax for the echo command is:

To print multiple variables, encase them in double quotes, separated by spaces:

echo "$VAR1 $VAR2 $VAR3"
  • The syntax for the printenv command is:

To get an overview of every single assigned environment variable in the current environment, run printenv without any arguments:

Printing all environment variables in the Z shell.

You can also pass multiple variables to the printenv command. For example:

printenv PATH USER
Print multiple variables using the printenv command.

The command prints the values of the specified variables.

Note: Learn two different methods on how to change Zsh to Bash on Mac.


This tutorial showed how to set environment variables using the Z shell. Environment variables are a convenient way to define how apps and processes will behave in the system without changing their underlying code.

Next, learn about the 8 types of Linux shells or see how to print variables in Bash using printf.

Was this article helpful?
Bosko Marijan
Having worked as an educator and content writer, combined with his lifelong passion for all things high-tech, Bosko strives to simplify intricate concepts and make them user-friendly. That has led him to technical writing at PhoenixNAP, where he continues his mission of spreading knowledge.
Next you should read
Bash eval Command with Examples
June 6, 2023

The bash eval command evaluates and executes strings as shell commands. This guide shows how to use the eval command in Bash.
Read more
How to Increment and Decrement Variable in Bash
June 1, 2023

This guide utilizes Bash scripts to show how to use different operators to increment and decrement a variables.
Read more
bashrc vs. bash_profile: What Is the Difference?
September 22, 2022

The Bash shell holds custom information in .bashrc and .bash_profile. Find out the difference between the two and when to use which file in this guide.
Read more
How to Read Files Line by Line in Bash
March 17, 2022

This tutorial shows how to read a file line by line using the Bash shell. See five different methods and process a file's contents effectively.
Read more