How to Export Environment Variables Permanently in Zsh
Permanently set environment variables across all shell sessions using Zsh configuration files.
Environment variables set with export only persist in the current shell session. When you close your terminal or open a new window, those variables disappear. Here’s how to make them permanent across all future shell sessions.
Step 1: Choose the Right Configuration File
Zsh reads multiple configuration files at startup. For environment variables that should be available in all sessions, use ~/.zshrc:
1
2
# ~/.zshrc is sourced for interactive shells
# Perfect for environment variables and aliases
Step 2: Add the Export Command
Open ~/.zshrc in your preferred editor and add the export command:
1
2
3
4
5
# Using nano (terminal editor)
nano ~/.zshrc
# Or using VS Code
code ~/.zshrc
Add your environment variable:
1
2
3
4
5
6
# Example: Disable ripgrep's built-in search
export USE_BUILTIN_RIPGREP=0
# Other common examples
export PATH="$HOME/bin:$PATH"
export EDITOR="code"
Step 3: Apply Changes Immediately
Reload the configuration to apply changes without restarting:
1
source ~/.zshrc
Or simply close and reopen your terminal.
Verification
Verify the variable is set correctly:
1
2
3
4
5
# Check if the variable exists
echo $USE_BUILTIN_RIPGREP
# List all environment variables containing the name
env | grep USE_BUILTIN_RIPGREG
Output should show: 0
When to Use Different Files
~/.zshrc (Interactive Sessions)
- Best for: Most environment variables, aliases, functions
- Loaded: Every time you open a new terminal
- Example:
export EDITOR="code"
~/.zprofile (Login Sessions)
- Best for: PATH modifications, system-wide settings
- Loaded: Only when you log in (first terminal launch)
- Example:
export PATH="/usr/local/bin:$PATH"
~/.zshenv (All Sessions)
- Best for: Variables needed by scripts and programs
- Loaded: For every Zsh session, even non-interactive
- Example:
export JAVA_HOME="/usr/local/java"
Troubleshooting
Variable Not Set
1
2
3
4
5
# Check if file exists and is readable
ls -la ~/.zshrc
# Verify syntax in zshrc
zsh -n ~/.zshrc
Wrong File Being Used
1
2
3
# Check which configuration file was loaded
echo $0 # Shows current shell
echo $ZSH_VERSION # Confirms Zsh is running
Need System-Wide Variables
For variables needed by all users, edit /etc/zshrc (requires sudo):
1
sudo nano /etc/zshrc
Important Notes
- Variables in
~/.zshrcpersist across all interactive shell sessions - Changes only affect new terminals - existing sessions need
source ~/.zshrc - Order matters: files are loaded in sequence
.zshenv → .zprofile → .zshrc - Use quotes around values with spaces:
export PATH="/path with spaces:$PATH"
This approach ensures your environment variables are consistently available across all shell sessions on macOS with Zsh.
☕ Support My Work
If you found this post helpful and want to support more content like this, you can buy me a coffee!
Your support helps me continue creating useful articles and tips for fellow developers. Thank you! 🙏