o7planning

Setup environment variables on Mac Os

  1. List environment variables
  2. Check for a specific environment variable
  3. Setup temporary environment variables
  4. Set permanent environment variables for specific user
  5. Set system-wide permanent environment variables
Environment variables are global variables used by users and processes in the operating system. In MacOS, there are two types of environment variables: temporary environment variables and permanent environment variables.
In this article, I will guide you to view, add, update and delete environment variables in MacOS.

1. List environment variables

To list current environment variables use the command below in Terminal:
printenv
The results you get:
karakol@karakols-MBP ~ % printenv
TMPDIR=/var/folders/_r/1pd7_2vs4hs1g9kc6hdj171w0000gn/T/
__CFBundleIdentifier=com.apple.Terminal
XPC_FLAGS=0x0
TERM=xterm-256color
SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.0NAIN66XMc/Listeners
XPC_SERVICE_NAME=0
TERM_PROGRAM=Apple_Terminal
TERM_PROGRAM_VERSION=444
TERM_SESSION_ID=2DDF4F68-006D-41D1-9DC2-D72AB013CA29
SHELL=/bin/zsh
HOME=/Users/karakol
LOGNAME=karakol
USER=karakol
PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
SHLVL=1
PWD=/Users/karakol
OLDPWD=/Users/karakol
LC_CTYPE=UTF-8
_=/usr/bin/printenv

2. Check for a specific environment variable

If you want to display the value of a specific environment variable just use the "echo" command:
echo $[variable name]
For example:
echo $PATH
Output:
karakol@karakols-MBP ~ % echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

3. Setup temporary environment variables

Temporary environment variables are set during a session and persist for that session; they are removed after the session ends.
When you open Terminal and create an environment variable, this environment variable exists only in this Terminal window, and is not shared with other Terminal windows. Temporary environment variables are useful so you don't have to type the same value multiple times when working with Terminal.
export [variable_name]=[variable_value]
For example, setup an environment variable, and view its value:
karakol@karakols-MBP ~ % export greeting="Hello Everybody"
karakol@karakols-MBP ~ % echo $greeting
Hello Everybody
karakol@karakols-MBP ~ % export greeting=Hi
karakol@karakols-MBP ~ % echo $greeting
Hi
For example, add a directory path to the system's current PATH environment variable.
karakol@karakols-MBP ~ % export PATH=/Users/test/test_folder:$PATH
karakol@karakols-MBP ~ % echo $PATH
/Users/test/test_folder:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

4. Set permanent environment variables for specific user

Permanent environment variables for specific users are stored in file. The location of this file depends on the Mac OS version you are using.
For MacOS versions older than Mac Catalina (10.15):
File
(Mac < 10.15)
Description
~/.bashrc
The environment variables set in this file are used for non-login Shells, such as when you open a new Terminal.
~/.bash_profile
The environment variables set in this file are used for login Shells, such as SSH.
In most cases, it is recommended to add environment variables to this file, as it ensures the variables are available to both login and non-login Shells.
With Mac Catalina (10.15) or later.
File
(Mac >= 10.15)
Description
~/.zshrc
The environment variables set in this file are used for non-login Shells, such as when you open a new Terminal.
~/.zprofile
The environment variables set in this file are used for login Shells, such as SSH.
In most cases, it is recommended to add environment variables to this file, as it ensures the variables are available to both login and non-login Shells.
You can check your MacOS version.
  • Apple Icon > About This Mac
Perhaps most people at this time are using MacOS versions newer than 10.15.
Run the following command to check the location of the .zprofile file:
~/.zprofile
Maybe this file does not exist, and you will receive the following message:
karakol@karakols-MBP ~ % ~/.zprofile
zsh: no such file or directory: /Users/karakol/.zprofile
If the above file does not exist, you just need to create it:
touch ~/.zprofile
Then open this file to modify the content.
sudo nano ~/.zprofile
  • Ctrl + O --> Save
  • Ctrl + X --> Exit
Note: In some versions of Mac OS, environment variables in files are not read each time you start a new session. In that case, every time you start a new session you have to read this file manually:
source ~/.zprofile
Print out the environment variable value you just set:
karakol@karakols-MBP ~ % source ~/.zprofile
karakol@karakols-MBP ~ % echo $greeting    
Hello

5. Set system-wide permanent environment variables

System-wide permanent environment variables are used by all users on MacOS, they are set in a file (depending on the operating system version).
With MacOS < Mac Catalina (10.15):
File
(Mac < 10.15)
Description
/etc/bashrc
The environment variables set in this file are used for non-login Shells, such as when you open a new Terminal.
/etc/profile
Environment variables set in this file are used for login Shells, such as SSH.
In most cases, it is recommended to set environment variables in this file, as it ensures the variables are available to both login and non-login Shells.
Với Mac Catalina (10.15) hoặc mới hơn
File
(Mac >= 10.15)
Description
/etc/zshrc
The environment variables set in this file are used for non-login Shells, such as when you open a new Terminal.
/etc/zprofile
Environment variables set in this file are used for login Shells, such as SSH
In most cases, it is recommended to set environment variables in this file, as it ensures the variables are available to both login and non-login Shells.
On Terminal, use the vim or nano command to edit file content directly:
sudo nano /etc/zprofile
  • Ctrl + O --> Save
  • Ctrl + X --> Exit
Make changes effective immediately:
source /etc/zprofile