How to Set Environment Variables in Zsh (Step-by-Step)
If you’ve stumbled upon Zsh and are wondering how to set up environment variables, you’re in the right place. Whether you’re a beginner or just looking to polish your terminal-fu, this guide will walk you through, step by step.
TL;DR: You can set environment variables in Zsh using either temporary or permanent methods. Temporary ones last only for a session. Permanent ones are added to config files like ~/.zshrc. Use export to make them available to subprocesses. Got it? Let’s dive in!
⚙️ What Are Environment Variables?
Table of Contents
Before we jump into Zsh specifics, let’s break it down.
Environment variables are like sticky notes for your computer. They tell programs where to find stuff, which user you are, or even what your favorite editor is.
For example, PATH tells your terminal where to look when you type a command. Without it, your terminal wouldn’t know where to find programs like ls or python.
🧠 Common environment variables
PATH– where your system looks for executablesHOME– your home directory locationUSER– your usernameEDITOR– your default command line editorLANG– system language and encoding
🔒 Setting Variables Temporarily
If you just want a variable for one terminal session, do this:
export MY_NAME="Zsh Hero"
Now try:
echo $MY_NAME
You should see:
Zsh Hero
Easy, right? But if you close the terminal, poof! It disappears.
🗂️ Making Them Permanent (Yes, Please!)
Want that variable to be there every time you open Zsh? Add it to your .zshrc file.
Step-by-step:
- Open your terminal
- Type
nano ~/.zshrcor open it with your favorite editor - Add your export line, like:
export MY_NAME="Persistent Zsh Fan" - Save and exit (in nano, it’s
Ctrl + OandCtrl + X) - Then run:
source ~/.zshrcThis reloads your config so the change takes effect immediately.
Now the variable is available every time you open up a new terminal window. Sweet!
🚀 Customize Your PATH
This one’s a common task. Let’s say you’ve installed a tool in a special folder, and you want your terminal to find it.
Example:
export PATH="$HOME/bin:$PATH"
This adds $HOME/bin to the front of your PATH. You can put this line in your .zshrc file to make it permanent.
But Wait… What’s with the $PATH part?
Smart question! You’re appending the existing PATH to avoid removing everything else. That way, the system keeps working as expected.
🎯 Checking Your Variables
Want to see if a variable exists?
echo $MY_NAME
Or list all environment variables?
printenv
Need to debug?
env
They both show similar things, and they’re super helpful if your environment feels “off.”
🧼 Unsetting Variables
Change your mind? You can remove a variable like this:
unset MY_NAME
Poof! It’s gone from the session. If it was in .zshrc, also delete or comment out that line and source the file again.
If you’re feeling cautious, comment it out like this:
# export MY_NAME="No More Zsh Hero"
📁 Zsh vs Bash: What’s Different?
If you’ve used Bash before, most of this probably looks familiar. That’s because Zsh and Bash are closely related when it comes to setting environment variables.
The main difference? Zsh typically uses .zshrc instead of .bashrc, and it offers some fancier scripting options, too.
🧙♂️ Bonus: Set Variables Conditionally
You can get fancy. For example, only set a variable if it’s not already set:
export MY_APP_HOME="${MY_APP_HOME:-$HOME/myapp}"
This says “use what’s already there, or default to $HOME/myapp.” Super useful if you share scripts or manage teams.
📚 Best Practices
- Keep environment variables lowercase in local scripts
- Use uppercase for global system-wide variables
- Don’t store sensitive info like passwords in plain text
- Group related config in blocks with comments
- Source your
.zshrcafter updates!
⛑️ Troubleshooting Tips
If something isn’t working:
- Check spelling in
.zshrc - Did you quote your strings correctly? Use
"not just any characters - Run
source ~/.zshrcto apply changes - Use
echo $VARIABLE_NAMEto check values - Restart your terminal session if needed
🎉 Wrapping Up
Congrats! You’re now an environment variable wizard in Zsh land. From setting variables temporarily to customizing your PATH like a boss, you’ve got it all under control.
Make sure to experiment. Make mistakes. That’s how you learn. And soon enough, your terminal won’t feel like a black box—but a magic box filled with your own settings and tweaks.
Go forth and Zsh!
