How to Set Environment Variables in ZSH

Environment variables are useful when working with scripts. They allow you to store values that can be reused without having to enter them repeatedly. This practice makes tasks more efficient. However, if you switch to a different shell, such as ZSH, setting up environment variables can become more complex. Each shell has its own way of handling these variables, so you may need to learn the specific commands for ZSH to keep everything running smoothly.

In this tutorial, we’ll demonstrate how to set environment variables in ZSH temporarily or permanently. 

Setting Environment Variables in ZSH

We can set environment variables in ZSH either temporarily using the export command or permanently by modifying the config file. Let’s go through each method one by one.

1. Setting Environment Variables in ZSH Temporarily

To set an environment variable temporarily, use the Linux export command directly in the terminal. This method will remain effective only for the current terminal session. To execute this command, you need to follow this syntax:

export MY_VAR="value"

Replace the value with the actual value. For instance, in the following example, we’re setting the environment variable DatabaseURL with the value mysql://user:password@localhost:3306/mydatabase:

export DatabaseURL ="mysql://user:password@localhost:3306/mydatabase"
setting the environment variable temporarily

This variable will be available only until you close the terminal or open a new session. Now to verify if the variable is specified correctly, run the following command:

echo $DatabaseURL

This command returns the value set for the environment variable:

confirm environment variable value

2. Setting Environment Variables in ZSH Permanently

To make an environment variable permanent, you need to edit the Zsh configuration file .zshrc so the variable is available each time you open a new terminal. Here’s a stepwise guide on setting up environment variables in Zsh permanently:

Step 1: Access the .zshrc File

Let’s access the .zshrc file in any editor like Nano:

nano ~/.Zshrc

Step 2: Set Environment Variables Z Shell

After accessing the .zshrc file, specify your environment variable. For example, the following line sets the environment variable for USER_NAME to the value “Anees”:

export USER_NAME="Anees"
setting environment variable permanently

After specifying the environment variable press CTRL + O to save the changes and then CTRL + X to exit.

Step 3: Apply the Changes

To apply the changes to the configuration file, use the source command:

source ~/.zshrc
apply changes to the zshrc file

Step 4: Verify the Environment Variable

Finally, you can verify the specified environment variable by executing the following command:

echo $USER_NAME

Now, the environment variable will persist permanently:

verify envirment variable

This is how you can set environment variables in the Z shell temporarily or permanently.

Conclusion

Setting environment variables in Zsh is a simple yet effective way to simplify your workflow, especially when working with scripts. You can temporarily set variables for the current session using the export command, or you can make them permanent by modifying the .zshrc file. In this article, we discussed both these methods so that you can configure your Zsh environment to meet your needs, whether for one-time use or long-term convenience. 

We hope this tutorial has helped you set environment variables in Zsh. You can rent a VPS from Ultahost to easily set environment variables in Zsh and manage your Linux environment. With scalable and flexible options, you can choose the resources that fit your needs, backed by our reliable service and support.

FAQ

What are environment variables?
What’s the difference between setting environment variables temporarily and permanently in Zsh?
How do I set an environment variable temporarily in Zsh?
How can I make an environment variable permanent in Zsh?
How do I verify if an environment variable is set correctly in Zsh?
Can I edit the .zshrc file with any text editor?
What if I accidentally set the wrong value for an environment variable?

Related Post

How to Generate Report Using MTR on Linux

When troubleshooting network issues on Linux, having a ...

How to Fix Broken Packages in Kali Linux

Kali Linux a security-focused distribution relies heavi...

Guide to Installing Commands on CentOS

When a Windows user switches to Linux the first thing i...

How to Use apropos Command in Linux

The apropos command in Linux is an essential tool for b...

How to Use the Rsync Command in Linux

Rsync, which stands for "remote sync," is a powerful an...

How to Install Git on Ubuntu

Git is an important tool on Ubuntu for both development...

Leave a Comment