macOS: How to Set Environment Variable

On the Mac, environmental variables store information, like who the current user is, default paths to commands and the name of the host computer. If you find yourself needing to create new environment variables, below we’ll cover the command lines in Terminal to set the value of the variables.

Opening Terminal

To start, open Terminal. You can go in through Go > Utilities > Terminal:

You can also open the Terminal through Spotlight. Hit the Command key and then the space bar at the same time to open Spotlight. Type Terminal and hit enter for Terminal to open.

Commands for Setting Environment Variables

You can create environment variables to control the behavior of a command. Apple’s support page states that this does not modify the command itself. Apple gives an example of using an environmental variable to set up a command print debug.

Apple states, “To set the value of an environment variable, use the appropriate shell command to associate a variable name with a value.”

For example, Apple gives the following command to set the variable PATH to a value:

% PATH=/bin:/sbin:/user/bin:/user/sbin:/system/Library/ export PATH

Here, the value would be

/bin:/sbin:/user/bin:/user/sbin:/system/Library/

Adding “% PATH=” before the value and “export PATH” after the value sets the variable PATH.

According to Apple, this command can help you view all environmental variables:

% env

Variables set in any individual Terminal window are not available after you close that window. If you want to keep the variable, you have to set up a shell startup script. You can find information for how to do this on the zsh man page under Invocation. The Apple support page has a link at the bottom of the page that opens the man page in your Terminal.

Was this helpful?

Thanks for your feedback!

Last updated on 22 June, 2023

Leave a Reply

Your email address will not be published. Required fields are marked *

The article above may contain affiliate links which help support Guiding Tech. The content remains unbiased and authentic and will never affect our editorial integrity.