Introduction
Environment variables are used to store system-wide values that can be used by any user and process under the operating system. Setting environment variables is essential in the steps of certain installations we covered such as How To Install Java or How To Install Java On Raspberry Pi.
This tutorial explains how to view, add, and remove temporary and permanent environment variables on macOS.
Prerequisites
- A system running the lastest version of macOS
- Access to the terminal
- A user with admin-level privileges
Check Current Environment Variables
There are two ways to check current environment variables in macOS:
1. Display and review a list of all current environment variables.
2. Display and review a specific environment variable.
List All Environment Variables
Use the printenv
command to display a list of currently set environment variables:
printenv
Note: If you want to display the complete list of shell variables, use the set
command.
Check A Specific Environment Variable
If you want to display the value of any specific environment variable, use the echo
command:
echo $[variable name]
For example, to check the value of the PATH
variable which stores a list of directories with executable files, use the
echo
command:
echo $PATH
Note: Always use the $
prefix when specifying a variable name.
Set Temporary Environment Variable
The value you assign to a temporary environment variable only lasts until you close the terminal session. This is useful for variables you need to use for one session only or to avoid typing the same value multiple times.
Assign a temporary environment variable with the export
command:
export [variable_name]=[variable_value]
Where:
-
-
[variable_name]
: The name for the new temporary environment variable you want to set.[variable_value]
: The value you want to assign to the new variable.
-
The export
command also allows you to add new values to existing environment variables:
export [existing_variable_name]=[new_variable_value]:$[existing_variable_name]
Where:
-
-
[existing_variable_name]
: The name of the environment variable you want to add a new value to.[new_variable_value]
: The value you want to add to an existing variable.
-
For example, if you want to add a custom folder path to the PATH
variable, use:
export PATH=/Users/test/test_folder:$PATH
Set Permanent Environment Variable
Permanent environment variables are added to the .bash_profile
file:
1. Find the path to .bash_profile
by using:
~/.bash-profile
2. Open the .bash_profile
file with a text editor of your choice.
3. Scroll down to the end of the .bash_profile
file.
4. Use the export
command to add new environment variables:
export [variable_name]=[variable_value]
5. Save any changes you made to the .bash_profile
file.
6. Execute the new .bash_profile
by either restarting the terminal window or using:
source ~/.bash-profile
Unset Environment Variable
Use the unset
command to remove an environment variable:
unset [variable_name]
Conclusion
After following this tutorial, you should know how to set temporary and permanent environment variables in macOS. This should make it easier for you to configure software packages in the future.
Looking for a different OS tutorial? Check out our guides on How To Set Environment Variables In Linux and How To Set Environment Variables In Windows.
Next you should also read
How to Set Environment Variables in Linux
December 17, 2020
Environment variables play a significant role in setting up the system and shell configuration at the…
November 5, 2020
Redis is an open-source solution for data storage, most often used as a database, cache storage, or a message…
SysAdmin,DevOps and Development
How to Check Java Version on Mac or Windows
August 24, 2020
Need to find which version of Java is installed on your system? This guide shows you how to do so, using the…
SysAdmin,DevOps and Development
How to Install and Get started with Git on Mac
February 4, 2020
Git is a version control system that allows developers to track a project and actively contribute without…
Author
Aleksandar Kovačević
Aleksandar Kovacevic is an aspiring Technical Writer at phoenixNAP. With a background in both design and writing, he aims to bring a fresh perspective to writing for IT, making complicated concepts easy to understand and approach.