Skip to content
New — orchestrate a team of Mel agents from your terminal. Learn more about orchestrating Mel agents
← All guides

Guides · terminal

How to set an environment variable on Mac, Linux and Windows

By Lalla Asmaa Alaoui, founder of Mel · Updated

In bash or zsh on Mac and Linux, run export NAME=value; it lasts until you close that terminal. On Windows, use $env:NAME="value" in PowerShell or set NAME=value in Command Prompt for the current window, and setx NAME "value" to keep it for new windows.

Environment variables are how many tools read settings and API keys. The trick is knowing whether you want a variable for one command, one session, or every session from now on. The steps below cover all three on each system.

Mac and Linux (bash and zsh)

  1. For one command only. Put the variable in front of the command:
NODE_ENV=production npm run build

The variable exists only while that command runs.

  1. For this terminal session.
export API_BASE_URL="https://api.example.com"

Every command you run in this window can now read it. Close the window and it is gone.

  1. Check it.
echo "$API_BASE_URL"
printenv API_BASE_URL
  1. Make it last. Add the export line to your shell's startup file. On a Mac, the default shell is zsh, so use ~/.zshrc. Most Linux systems use bash, so use ~/.bashrc. Not sure? Run echo $SHELL.
echo 'export API_BASE_URL="https://api.example.com"' >> ~/.zshrc
source ~/.zshrc

source loads the change into the current window. New windows pick it up on their own.

  1. Remove it.
unset API_BASE_URL

Also delete the line from your startup file if you added one.

If you use fish, the syntax differs: set -gx NAME value for the session, and set -Ux NAME value to keep it.

Windows

  1. PowerShell, for this window.
$env:API_BASE_URL = "https://api.example.com"
$env:API_BASE_URL

The second line prints the value to check it.

  1. Command Prompt, for this window.
set API_BASE_URL=https://api.example.com
echo %API_BASE_URL%

Do not put spaces around =. In Command Prompt they become part of the name or value.

  1. Keep it for your user account.
setx API_BASE_URL "https://api.example.com"

setx saves the variable for future windows, but not the one you are in. Open a new terminal to see it. Values longer than 1,024 characters are cut off, so do not use setx to grow a long PATH; use the Environment Variables dialog in Windows settings instead. The PowerShell equivalent is:

[Environment]::SetEnvironmentVariable("API_BASE_URL", "https://api.example.com", "User")

API keys need extra care

Many guides tell you to put an API key in an environment variable. That works, but:

  • Never commit a key. If you keep keys in a .env file, add .env to .gitignore before your first commit.
  • Startup files are plain text. Anyone who can read your ~/.zshrc can read the key. A password manager's command line tool can load it when needed instead.
  • Use one key per tool, so you can revoke one without breaking the rest.

Doing it with Mel

Mel runs your own shell (your $SHELL on Mac and Linux, cmd.exe on Windows), so the commands above work at its prompt. On Mac and Linux, when Mel starts, it loads the environment from your login shell once, so variables you export in ~/.zshrc or ~/.bashrc are there even if you opened Mel from the dock or a desktop icon. If you change your startup file, restart Mel to pick it up.

Mel Drive adds named environment variable sets. Create one from the Drive + menu, then click Load to export the whole set into the current pane only. A value can be plain text, the output of a command, or a reference that 1Password or LastPass fills in when you load it, so the secret does not have to sit in the file. Values are masked on screen until you reveal them.

For Mel's own AI agent you do not need an environment variable at all. Add your provider key in Settings → AI → Provider API keys. Mel stores it in your system keychain on Mac and Windows, or in a file only your user can read on Linux.

FAQ

Why does my variable disappear when I open a new terminal? export and set only last for the current window. Add the line to ~/.zshrc or ~/.bashrc, or use setx on Windows.

Why does setx not work in the current window? setx writes the value for future sessions only. Open a new terminal, or also run set or $env: for the current one.

How do I see all environment variables? Run env or printenv on Mac and Linux, set in Command Prompt, or Get-ChildItem Env: in PowerShell.

Should I use .bashrc or .bash_profile? On Linux, ~/.bashrc is the usual place. On a Mac, if you use bash rather than zsh, login windows read ~/.bash_profile, so many people add source ~/.bashrc there.

Is it safe to keep API keys in environment variables? It is common, but keep them out of git and out of shared files. A password manager or a tool's own key storage is safer.

1Password and LastPass are trademarks of their owners. Mel is not affiliated with or endorsed by 1Password or LastPass.

Mel – the agentic terminal · openmel.dev. A multi-agent, multi-model AI terminal for Mac, Windows, Linux and VS Code. Free with your own API key.

Lalla Asmaa Alaoui
Written by
Lalla Asmaa Alaoui
Founder & CEO of Mel

Lalla is a hands-on tech founder, agentic infrastructure architect and the vision behind Mel.

Follow on LinkedIn

Get started

Try the agentic terminal.

The Mel terminal is free to download, no account needed. The AI agent is free on your own provider key — 1,500 hosted-AI credits a month on Pro.