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

How to set environment variables in ZSH Picture 1

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

echo $MTE

How to set environment variables in ZSH Picture 2

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:

How to set environment variables in ZSH Picture 3

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

How to set environment variables in ZSH Picture 4

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

echo $VARIABLE_NAME

How to set environment variables in ZSH Picture 5

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

printenv VARIABLE_NAME

How to set environment variables in ZSH Picture 6

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

 

printenv

How to set environment variables in ZSH Picture 7

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.

4 ★ | 1 Vote

May be interested

  • Endlocal command in WindowsEndlocal command in Windows
    the endlocal command ends the localization of the environment changes in a batch file, and restores the environment variables to their original values ​​before the corresponding setlocal command is run.
  • Variable in JavaScriptVariable in JavaScript
    one of the most distinctive features of a program language is the set of data types it supports. this is the type of value that can be represented and manipulated in the program language.
  • Setx command in WindowsSetx command in Windows
    the setx command helps create or modify environment variables in a user or system environment, without requiring programming or script writing.
  • What is the staging environment?What is the staging environment?
    the staging environment is an exact replica of the production environment for software testing. the staging environment was created for code testing, builds and updates.
  • Use variables in ShellUse variables in Shell
    a variable is a string of characters from which we assign a value. the assigned value can be a number, text, file name, device or any other type of data.
  • Scope rules in programming CScope rules in programming C
    a scope in any program is an area in the program where a defined variable exists and outside of that range the variable cannot be accessed.
  • stddef.h in Cstddef.h in C
    the file header named stddef.h in c library defines various types of variables and macros. many of these definitions are also present in other headers.
  • Set command in WindowsSet command in Windows
    the set command helps show, set or remove cmd.exe environment variables.
  • How to protect your mental and physical health when in a toxic environmentHow to protect your mental and physical health when in a toxic environment
    dealing with toxic situations can be incredibly difficult. here are some tips to help you handle toxic environments tactfully and maintain peace of mind.
  • Research shows that smartphones are the most destructive environmental factorsResearch shows that smartphones are the most destructive environmental factors
    a recent study shows that smartphones and data centers will be the most damaging factors for the environment by 2040.