How to set environment variables


What are environment variables?

Environment variables are a mechanism for naming and holding values ​​with OS functions, and are a common mechanism for multiple OSs such as Windows, macOS, and Linux.

Environment variables consist of names and values, and the OS or application can share values between the OS and multiple applications by retrieving values using the name of the environment variable.

One example of an environment variable is PATH.

If you download the application compressed with the Zip file from the Internet, there may be a procedure such as “Set the directory where the application is decompressed in the PATH environment variable”.

In the PATH environment variable, the paths of multiple directories are set, separated by “:” for macOS and Linux, and separated by “;” for Windows.

The directory set in this PATH environment variable is used as the search destination when only the file name is specified by the OS or application.

For example, you can simply enter the application name on the command line to run it because the PATH environment variable has a directory of application execution files.

Environment variables are often set automatically by the installer, but depending on the application, there is such a thing that you have to set it manually, so I think it is better to understand the basic setting method.

Set up environment variables

macOS /Linux

For macOS and Linux, the way environment variables are set is basically the same.

You can set the value to the environment variable with the following command.

export [Environment Variable Name]=[Value]

The following is an example of adding “~/app/” to the PATH environment variable after the value set to the current PATH environment variable.

export PATH=$PATH:~/app/

However, although the above command is valid while the terminal is running, the setting disappears when the OS is restarted or the terminal is closed, so the environment variable must be set every time.

To enable the set environment variables automatically, it is necessary to set the environment variables in the shell configuration file.

The configuration file differs depending on the shell you are using, so check the shell you are using with the following command.

echo $SHELL

If the command execution result is “/bin/bash”, set the environment variable in either the bash configuration file ~/.bashrc or ~/.bash_profile.

If the command execution result is “/bin/zsh”, set the environment variable in either the bash configuration file ~/.zshrcor ~/.zshenv.

Create the configuration file with a text editor such as vi or nano, or open the existing file if it exists, and describe the required environment variables in the format of export [Environment Variable Name] = [Value].

You can also write to the configuration file with the command by executing the command as shown below.

echo "export PATH=$PATH:~/app/" >> "~/.bashrc"

For Linux, the default shell is bash, depending on the distribution.

For maxOX, the default shell has been changed from bash to zsh since catalina.


In the case of Windows, you can set it from gui, command prompt, and PowerShell, but here we will explain how to set it from PowerShell.

You can set the value of the environment variable by executing the following command in PowerShell.

[System.Environment]::SetEnvironmentVariable([Environment Variable Name], [Value], "Machine")

The following is an example of adding “C:\app” to the environment variable Path after the value set in the current environment variable Path.

$path = [System.Environment]::GetEnvironmentVariable("Path", "Machine")
$path += ";c:\app"
[System.Environment]::SetEnvironmentVariable("Path", $path, "Machine")

In the above example, [System.Environment] :: GetEnvironmentVariable ("Path", "Machine") is used to get the value of the current Path environment variable.

After that, $path += ";c:\app" is used to combine “C:\app” after the current environment variable, and then the value is set in the environment variable.