Environment variables are a way to store values on the local machine (the one running the program). For example, you can store a user ID that would be used for logging in to a system. You can also use environment variables to store configuration settings like the name of a database connection.
When setting environment variables, it is important to consider the security implications. If an attacker is able to access your environment variables, they could gain access to sensitive information such as your username and password. Therefore, it is important to keep your environment variable names secure.
Those new to mac can get overwhelmed with how to set up and manage these environment variables. This guide provides easy ways to do so.
View the current environment variables
To view all your environment variables, open a terminal on macOS and type:
We can also use the echo command to print the specific environment variable values:
echo $JAVA_HOME/usr/local/Cellar/[email protected]/188.8.131.52/libexec/openjdk.jdk/Contents/Home
By default, most Linux distributions expose environment variables as key=value pairs (e.g., key=”value”). These pairs are normally stored in files like /etc/paths or /etc/defaults/key1 or in other locations. In some cases, they can be exposed by placing special entries in a shell’s .bashrc or .profile file.
Setting a temporary environment variable:
A temporary environment variable is a variable defined in the process buffer that exists only while the program runs. It is lost when the program ends or if the process terminates, and it is not available to other programs.
The value of the temporary environment variable is lost when the application process ends or when you terminate it. The value of a temporary environment variable can be retrieved with the Process Monitor tool.
Then print the value if MY_VAR with the echo command.
You will see the assigned value as the output. But, if you open a new terminal and try to print the same variable. You will see a blank line in the output.
So, the manually exported environment variable’s scope is the active shell only.
Setting a permanent environment variable:
A permanent environment variable is defined in an application’s configuration file. It persists even after fork and exec and is available for use by all processes in the same execution context (i.e., all processes on a single computer). Permanent environment variables are not lost when the application process ends or when you terminate it.
Since the latest macOS uses the Zsh shell, So we can add the environment variables to the ~/.zshrc or ~/.zshenv file for the current user. For example, to create the JAVA_HOME environment variable and also update PATH to include JAVA_HOME. Edit
~/.zshenv and add the following values;
~/.zshevnexport JAVA_HOME=$(/usr/libexec/java_home -v11) export PATH="$JAVA_HOME/bin:$PATH"
The first line is setting the JAVA_HOME environment variable and the second command is prefixing the Java bin directory to the PATH environment variable.
Older than macOS 10.15 uses the Bash shell as default. That user’s edit ~/bash_profile for adding permanent environment variables.