Mac OSX

How to Set Environment Variable in macOS Catalina

Developers mostly understand the meaning behind environment variable. The case you are here, you probably understand what an environment variable is. However, sometimes for running certain softwares and eliminating a few errors require changing an environment variable. Therefore, this article tells you ways to set environment variable in macOS Catalina.

What is an Environment Variable?

Environment variable is simply a variable with a stored value in operating system. It has a name and a stored value. There are different types of the environment variables, for example, variables storing path to certain libraries or local system settings. An environment variable can also store current working directory or default shell.

Method to set environment variable in macOS Catalina:

In order to set environment variable, we only need one application. Terminal is the utility which we are going to use. First of all, let’s enlist the already available environment variables. Use the following command in terminal:

printenv

If you want to view the value of a variable then use the following command:

echo $name_of_variable

Replace the name_of_variable with the variable of which you want to see the value. For example, echo $HOME would return the value of your home path.

Set permanent environment variable in macOS Catalina

Now, you know the environment variables in use. Also you know the method to read the value of those variables. Let’s learn how to change value of an environment variable permanently in Catalina. If you echo the value of Shell in Catalina, you’d see that you are using ZSH instead of Bash as compared with the previous versions of macOS. This also changes your .bash_profile and .bashrc paths, however, here we are dealing with env variables configuration, so let’s stick to it. Use the following command to edit the zshenv configuration.

nano ~/.zshenv

Using the following syntax, add the custom value of any environment variable in the file. For example, I am adding HOME variable value like below:

Set Custom Environment Variable on macOS

After writing your variable value change command using the syntax shown above; press CTRL+X and press Y to save the file.

export HOME=/Users/talkofweb/Documents:$HOME

Run echo command to be sure about the updated value of the environment variable. This was the method to permanently change the value of any environment variable in Catalina.

Set temporary environment variable in macOS Catalina

If you don’t want to set a permanent value of the environment variable in your macOS Catalina, then there is another method. You need to use the following command in the terminal to change the value of any variable. Here I am changing the value of $HOME variable temporarily.

export HOME=/Users/talkofweb/Documents:$HOME

The colon sign appends the new value of the variable which is defined after the equals to sign.

Conclusion:

This is how you can set a non-persistent or persistent environment variable in Mac OS Catalina. On the contrary, if you want to do the same in Linux then read: How to set environment variables in Linux. Surprisingly, the executable commands are the same. In Windows 10 the procedure is a little different but it’s as easy as in Linux and Mac OS.

2 Comments

  • Hi, after I modified .zshenv I get zsh:permission denied when trying to run a command. any ideas why?

    • Dear Doris,

      You might not be using administrative privileges, the denied return message is an error because you don’t have the required permissions.