How to set environment variables on a Mac

For the current terminal window, run export NAME=value. To keep it, add that same line to ~/.zshrc and open a new window. zsh has been the Mac's default shell since macOS Catalina, so ~/.zshrc is the file to edit unless you switched to bash. API keys are the exception: they're better kept out of that file, for reasons further down.

We make fidelius, a Mac app built on iCloud Keychain, with no vault or sync of its own, that keeps API keys out of shell and project files. It comes up at the end; everything before that works without it.

For one terminal window

$ export EDITOR=nano$ echo "$EDITOR"

The variable lasts until you close the window, and every command you start from that window gets it. To check what's set, printenv EDITOR shows one variable and env lists them all. unset EDITOR removes it.

To set a variable for a single command only, put it in front of the command:

$ EDITOR=nano git commit

Permanently, in ~/.zshrc

Append the line to the file, then load it into the window you have open:

$ echo 'export EDITOR=nano' >> ~/.zshrc$ source ~/.zshrc

Single quotes keep the line exactly as written, so a $ in it is expanded when zsh reads the file, not when you append it. To edit the file instead, run nano ~/.zshrc, which creates it if your Mac doesn't have one yet. Files starting with a dot are hidden in Finder; Command-Shift-. shows them. If you edit in TextEdit, turn off Edit > Substitutions > Smart Quotes first, or it curls the quotes and breaks the line.

Which zsh file to use

FileRead byUse it for
~/.zshenvEvery zsh, including scriptsRarely needed. Keep it small
~/.zprofileLogin shells, which is what Terminal opensPATH changes, such as Homebrew's
~/.zshrcEvery interactive terminalMost variables, aliases and prompts

zsh reads them in that order, so a line in ~/.zshrc wins over the same variable set earlier. If you use bash, the file is ~/.bash_profile.

Adding a folder to PATH

Put the new folder first and keep the rest of the existing PATH after it:

$ echo 'export PATH="$HOME/bin:$PATH"' >> ~/.zprofile

Leaving out :$PATH replaces the whole list, and commands like ls and git stop being found until you fix the file.

Apps opened from the Dock

An app you open from the Dock, Finder or Spotlight doesn't start a shell, so ~/.zshrc doesn't apply to it. That's why a variable can work in Terminal and be missing in a desktop app.

launchctl setenv EDITOR nano sets a variable for apps you open afterwards, until you log out or restart. To make it stick, people run that command from a login item or a LaunchAgent. Old guides mention ~/.MacOSX/environment.plist; current macOS ignores it.

Where API keys shouldn't go

A lot of setup guides tell you to put an API key there: export OPENAI_API_KEY=.... It works, and it has costs:

  • The key sits in plain text in a file in your home folder, often one that ends up in a dotfiles repo or a backup.
  • Every command you start from a terminal gets it, including AI coding agents and anything they run. One env in an agent session prints it into the conversation.
  • Every key is set at once, so two projects can't use different keys under the same name.

The safer pattern is to keep keys out of shell files and give each command only the keys it needs, when it starts. On a Mac the place for them is the Keychain, and the built-in security command can do it. Store the key once; with -w last it asks for the value, so the key stays out of your shell history:

$ security add-generic-password -a "$USER" -s openai-api-key -w

Then hand it to one command when that command starts:

$ OPENAI_API_KEY="$(security find-generic-password -s openai-api-key -w)" python app.py

Where fidelius fits

fidelius keeps API keys in your Keychain, grouped by project. accio myapp npm run dev runs one command with that project's keys as environment variables, and the rest of your terminal doesn't have them. With one click it adds instructions for Claude Code, Codex and Gemini CLI to use accio, so they work with key names instead of values, as long as they follow those instructions.