Introduction
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.
Prerequisites
- 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:
export VARIABLE_NAME=VARIABLE_VALUE
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:
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:
set VARIABLE_NAME=VARIABLE_VALUE
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:
export VARIABLE_NAME=VARIABLE_VALUE
3. Save the file and close the editor:
:wq
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
:
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:
echo $VARIABLE_NAME
To print multiple variables, encase them in double quotes, separated by spaces:
echo "$VAR1 $VAR2 $VAR3"
- The syntax for the
printenv
command is:
printenv VARIABLE_NAME
To get an overview of every single assigned environment variable in the current environment, run printenv
without any arguments:
printenv
You can also pass multiple variables to the printenv
command. For example:
printenv PATH USER
The command prints the values of the specified variables.
Note: Learn two different methods on how to change Zsh to Bash on Mac.
Conclusion
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.