navigation

How to set environment variables in ZSH

Environment variables are simple settings that control how a program runs on your computer. You can use them to change how the software behaves (without editing the actual code). ZSH or Z shell is a command-line shell used in Unix-like systems. It provides features like advanced auto-completion, customization options, command history, spelling correction, etc. In ZSH, you can easily set environment variables, either temporarily or permanently, depending on your needs. Let's see how we can do this in the following article!

Environment variables in ZSH

In Zsh, environment variables (ENVs) are key-value pairs that configure and control how programs and processes run. They provide essential information to the system and guide program behavior.

 

Environment variables are case sensitive, meaning uppercase and lowercase letters are treated differently. Environment variables are often written in uppercase to make them easier to identify.

Temporary vs. Permanent Environment Variables

Temporary variables are only active for the duration of the current terminal session. They are automatically deleted when you log out, close the terminal, or restart the system. On the other hand, permanent variables are stored in configuration files such as '~/.zshrc'. You can set a permanent environment variable by adding it to a configuration file, such as '~/.zshrc'. This ensures that the variable is automatically set and available every time you start a new terminal session.

How to set temporary environment variables

The export command is the easiest way to temporarily set environment variables. This command creates an environment variable for the current terminal session. You can use the following syntax to create a temporary environment variable in ZSH:

 

export VARNAME=VARVALUE

For example, running the following export command will create an MTE variable and assign it a value of '7':

export MTE=7

Use the printenv or echo command to verify the variable creation:

echo $MTE

Additionally, we can use the set command to set temporary variables:

set varName=varValue

With the set command, we can set multiple values ​​of a variable. But in that case, we need to separate each value with a colon ':', like this:

export VARNAME=VAL1:VAL2:VAL3

If you want to specify a space-separated value for a variable, enclose the value in double quotes:

export VARNAME="MAKE TECH EASIER"

How to set environment variables permanently in ZSH

To keep the environment variable active permanently, add it to the '.zshrc' file. If the file doesn't exist, you can create it using any text editor.

Execute the command below to create a new '.zshrc' file or edit an existing one in the Nano editor:

nano ~/.zshrc

Now, use the export command to add the new variable to the environment permanently:

 

export VARNAME=VALUE

Replace VARNAME and VALUE with their respective names and values:

Save and exit the file. Use the source command to apply the changes from the '.zshrc' file without restarting the terminal. Alternatively, you can close and reopen the terminal to start a new session with the updated changes:

source ~/.zshrc

Finally, you can verify that the environment variable is set permanently using the echo command as follows:

echo $VARIABLE_NAME

Similarly, you can use the printenv command to check environment variables:

printenv VARIABLE_NAME

Alternatively, you can use printenve without any arguments to get an overview of all variables in the current environment:

 

printenv

Best practices for managing environment variables in ZSH

Here are some recommended practices to improve variable management in Z Shell:

  1. Use clear and descriptive variable names that clearly describe their purpose (for example, JAVA_HOME, PROJECT_PATH).
  2. Use uppercase convention for environment variable names. This makes them easier to identify and avoids confusion with regular shell variables.
  3. If a value contains spaces or special characters, enclose it in double quotes to avoid errors. For example, export DOMAIN="MAKE TECH EASIER".
  4. Run source ~/.zshrc after making updates to apply immediately without restarting the terminal.
  5. Make a backup of your '.zshrc' file before making changes, especially if you are unfamiliar with the syntax.

This way you can set environment variables in ZSH efficiently.

Setting environment variables in ZSH is a simple yet powerful way to control how your system and applications behave. Whether you need to make temporary changes for the current session or want a variable to persist permanently, ZSH makes it easy to manage them through commands like export or by editing the '.zshrc' file. Just be sure to use clear and descriptive names, encapsulate values ​​in quotes when necessary, and apply changes properly using source. Also, be sure to understand how you can set and list environment variables in Linux to better manage your system configurations.

Update 26 May 2025