theme: ricetheme
New palette
New palette

Set Environment Variables on Mac: Every Method Explained

Set environment variables on Mac for lasting access across sessions, then choose between persistent zsh settings, launchctl, and PowerShell 7.

Flat design of a macOS terminal window with pastel prompt lines and a side settings panel on a crust-colored background.

tl;dr the short answer

Environment variables are named values that programs can read to get settings such as paths or app-specific locations. On a Mac, a Terminal export lasts only for that session, zsh startup files serve shell sessions, and a LaunchAgent can make a value available to apps opened after login; PowerShell variables belong in its profile to persist across sessions. Choosing the right place for each value prevents settings from disappearing or being unavailable to the apps that need them.

Ever wished you could set a value once and have every program you open just know it? That’s what an environment variable does. Think of it as a sticky note on your desk that every app in your session can read.

On a Mac, you can set these in several places, and each one lasts a different amount of time. We’ll cover every option, from quick terminal exports to persistent zsh setups, launchctl, and PowerShell.

If you’re coming from Linux, we’ll also map your .bashrc and /etc/environment habits onto macOS and flag where they differ.

Set Environment Variables on Mac That Persist

A plain export NAME=value in Terminal lasts only until you close that window, and a line in .zshrc reaches Terminal but not the apps you open from the Dock.

For a setting that survives restarts and reaches those apps, we’ll use a LaunchAgent, a config file that macOS loads at login.

Create a .plist file for your variable

Save a new .plist file (a structured-text settings file) in ~/Library/LaunchAgents, then edit the label and set the name and value for launchctl setenv:

<plist version="1.0">
<dict>
  <key>Label</key>
  <string>com.you.matlab-java</string>
  <key>ProgramArguments</key>
  <array>
    <string>/bin/launchctl</string>
    <string>setenv</string>
    <string>MATLAB_JAVA</string>
    <string>/path/to/java</string>
  </array>
  <key>RunAtLoad</key>
  <true/>
</dict>
</plist>

Only need it in Terminal? Add an export line to .zshrc instead. Use .zprofile for setup that runs once at login. If you’re coming from Linux, .zshrc is the rough equivalent of .bashrc, and .zprofile matches .profile.

A flat illustration of a dark-themed code editor window with syntax highlighting bars, a folder sidebar, and blank tabs.

How can I check variables in Terminal?

Run printenv to list your variables, or printenv MATLAB_JAVA for one. To apply a new file:

  1. Reboot, or run launchctl load on the file.
  2. Open a new Terminal session, since open windows keep their old environment.
  3. Check with printenv MATLAB_JAVA.

Who it affects and how to undo it

This variable belongs to your account alone, unlike /etc/environment on Linux, which applies to every user. To remove it:

  • run launchctl unload on the file,
  • delete the .plist,
  • and restart.

That’s all it takes.

Examples and fixes for common variable problems

  • MLM_LICENSE_FILE and MATLAB_JAVA: a Linux user moving to a Mac might set these in .zshrc, then find MATLAB launched from the Dock ignores them. A LaunchAgent fixes that for every app you open after login.
  • DYLD_LIBRARY_PATH rule changes: newer macOS handles it differently, so an older fix may quietly do nothing.
  • Variable names must be specific: generic names like LICENSE_FILE can collide with other tools, so pick one that names its app, like MATLAB_JAVA.
  • Changing PATH: PATH is your command search list, so prepend rather than overwrite, as in export PATH="$HOME/bin:$PATH". Replacing it outright can hide system commands.

To change a saved value, edit the .plist, then run launchctl unload and launchctl load on it. For a .zshrc export, open a new session instead.

Need a .env file? It’s a plain-text list of NAME=value lines that a specific tool reads. Create one with touch .env, then add lines like MATLAB_JAVA=/path/to/java. macOS won’t load it into your shell for you.

Flat illustration of a macOS terminal with Catppuccin Mocha colors and a document tile on a dark background.

PowerShell 7 Variables on MacOS

PowerShell 7 on a Mac will feel familiar to Linux users, but some ways of setting variables don’t last. Here’s how to make them stick.

Why SetEnvironmentVariable changes don’t last

A change made with [Environment]::SetEnvironmentVariable is a temporary setting, not a permanent one. It lasts only for the current session. Take a Linux admin on a new MacBook.

She sets $env:BACKUP_DIR this way, and the script fails the next morning in a fresh window. The variable never got saved. The fix is a file that runs whenever PowerShell starts.

Using your PowerShell profile and config file

Your PowerShell profile is a script that runs each time you start a new session, so anything you add there loads automatically. Run $PROFILE to see its exact path.

A powershell.config.json file may show up in guides, but it holds PowerShell’s own settings, not your variables. The admin from before adds that line once, and every new terminal tab has it ready.

Your terminal app may also pass in variables, and when a name shows up in both places, the result isn’t always obvious. Type $env:NAME to see what PowerShell actually uses. Check it, don’t guess.

Editing PATH so your modules load

For a quick test, $env:PATH changes only the current session. For anything you’ll rely on, add the change to the top of the module’s .psm1 file so it loads with the module.

Either way, avoid editing PowerShell’s default config, so your changes stay easy to undo.

Windows uses a semicolon as the path separator between PATH entries, while macOS uses a colon, so hardcoding one breaks the other. Use [System.IO.Path]::PathSeparator so one file works on both platforms.

Setting environment variables on a Mac comes down to picking the right layer for the job. A quick export lasts only as long as your current terminal session, while a zsh profile keeps the value ready for every new one.

Match the method to how long the value needs to live, and most of the guesswork disappears.

Bring your Linux habits along, and PowerShell is there if you prefer it. Set each value in the right place once, and your shells will simply know what you meant.

Questions

How to change env variables on Mac?

To change an environment variable for Terminal sessions, edit its `export NAME=value` line in `~/.zshrc`, then open a new Terminal window or run `source ~/.zshrc`. For a variable that should also reach apps opened from the Dock, update its LaunchAgent `.plist` and reload it.

How to set an env variable in terminal?

In Terminal, run `export NAME=value`, replacing `NAME` and `value` with your variable name and value. This setting lasts only for the current shell session and its child processes.

How do I create an .env file on a Mac?

Create an empty file in the current directory with `touch .env`, then open it in a text editor and add lines such as `MATLAB_JAVA=/path/to/java`. A `.env` file is plain text, and macOS does not automatically load it into your shell; the specific tool or application must read it.

How to set environment variable in mac zsh?

Add a line such as `export NAME=value` to `~/.zshrc` to set a variable for new zsh sessions. Apply the change in the current session with `source ~/.zshrc`, or open a new Terminal window.

How to see environment variables in Mac terminal?

Run `printenv` to list environment variables in the current Terminal session, or run `printenv NAME` to check one variable. You can also use `env` to list the current environment.