Or copy link
Copy link
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.
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.
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"
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:
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:
.zshrc
Set Environment Variables with our Linux VPS!
Set environment variables and practice Zsh commands with Ultahost’s cheap Linux VPS, powered by NVMe SSD storage to optimize your workflow.
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"
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
Read also How to Set Environment Variables in Linux
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:
This is how you can set environment variables in the Z shell temporarily or permanently.
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.
Environment variables allow you to store values that can be reused across different processes and scripts. This makes tasks more efficient and reduces the need to repeatedly enter values.
Temporarily setting an environment variable makes it available only for the current terminal session, while permanently setting it makes the variable available every time you open a new terminal by modifying the .zshrc file.
To set an environment variable temporarily, use the export MY_VAR=”value” command in the terminal.
To make an environment variable permanent, edit the .zshrc file and add the export command with the desired variable. Then, apply the changes using the source ~/.zshrc command.
You can verify an environment variable by using the echo $variableName command.
Yes, you can use any text editor, such as Nano, Vim, or others, to edit the .zshrc file. For example: nano ~/.zshrc edits the zshrc file in the nano editor.
If you set the wrong value, you can update the variable by editing the .zshrc file again or using the export command to change its value temporarily. To remove an environment variable, you can use the unset command.
When troubleshooting network issues on Linux, having a ...
Kali Linux a security-focused distribution relies heavi...
When a Windows user switches to Linux the first thing i...
The apropos command in Linux is an essential tool for b...
Rsync, which stands for "remote sync," is a powerful an...
Git is an important tool on Ubuntu for both development...
Save my name, email, and website in this browser for the next time I comment.
Δ