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.
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:
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.