Setting environment variables in a command line interface such as Mac Terminal provide an easy way to reference data without memorizing long strings of characters and numbers. It can also be necessary for settings paths and parameters for specific applications.
To set an environment variable in Mac Terminal, we can use the export command along with the variable name and the value, as shown below.
My name is Eric, and as a software engineer, I have worked with and written scripts in command line interfaces such as Mac Terminal for many years. Knowing how to set environment variables can be an essential part of working in command shells, and I can show you how to do this.
Keep reading if you would like to see how to set environment variables in Mac Terminal. I will also discuss why we need to use the export command, how to list the current variables in your environment and the benefits of using environment variables.
- Setting an Environment Variable
- Displaying the Value of the Environment Variable
- How to Display the Current Environment Variables
Setting an Environment Variable
An environment variable is a variable that can be set to a value within the environment of the current shell or Terminal session. Environment variables are also set within your operating system and available while your computer runs.
As we have seen above, setting an environment variable is relatively straightforward. All it takes is a simple export command, the variable name, the equal sign, and the value you wish to set it to, as shown below.
For example, if I want to create an environment variable called MYVAR and set it to a value of hello, I would use the below command.
We could just set the variable equal to the value without using the export command, but doing that would only allow the variable to exist within the thread it is created in, and it would not be available for use by any application, script, or user.
The export command exports the variable to be available to all processes and child processes running. Because of this, it is recommended that you always use the export command to set a variable.
You can set environment variables from the command line, and you can also set shell scripts or other types of scripts and programming languages. You will often see variables set in your .zshrc or .zprofile files in your home directory.
These are run at the startup of a terminal session and often set values for things like your prompt (PROMPT variable) and your path (PATH variable).
The path variable is often vital for running specific commands or applications from the command line because it tells the system where to go to find the command or application when you type it on the command line.
Displaying the Value of the Environment Variable
Now that you know how to set an environment variable, you might want to know how to display or show its value. This can be done from the command line using the echo statement below.
You might notice in the command above that we have added the $ character in front of the variable name. To reference or use the variable, we put the $ in front of it to tell the system that we want to use the variable’s contents. So to continue with our example:
How to Display the Current Environment Variables
When you start up your Terminal application, you will already have several environment variable set. You may set more yourself or change some existing ones using the export command.
You might wonder how to see the current environment variables and their values. It’s easy to do using the env command. Simply type env and then hit the return key. You will then see the whole list of environment variables and their values that are currently set.
The following are a few questions that are commonly asked when it comes to setting environment variables.
Why Do We Need Environment Variables?
Environment variables are used by your system and its applications so that it can share specific values and sometimes pass values between them. They can also be used to make things easier for the user. If I have a frequent directory that I go to, I could do the following.
Now, if I want to quickly reference or go to that directory, all I have to do is use the $MYDIR environment variable.
Can I change an environment variable?
Yes, you can change the value of an environment variable by just setting it again. Use the export command and set the variable equal to the new value. It’s that easy. You can set it and reset it as often as you need.
Can I set an Environment Variable to another Environment Variable?
You can set your environment variable to another existing variable. Make sure you use the $ in front of the variable from which you want to get the value. Remember, the $ gets the value contained in the variable.
export NEWVAR = $MYVAR
Can I remove an environment variable?
Yes, you can remove an environment variable using the unset command and the variable name, as shown below.
Using our example from above.
Environment variables are often necessary when working with the Terminal application on your Mac. Other applications and even macOS use them. They also make it easier to type commands with lots of data or parameters.
Setting an environment variable can be done using the export command from either the command line or from within code or a script. Environment variables can also be changed using the export command, and you can remove them using the unset command.
How do you use environment variables when working in Mac Terminal? Let me know; I would love to get your feedback.