- How to Set Environment Variable in Windows
- How to export and import environment variables in windows?
- How to export and import environment variables in windows?
- How to export and import environment variables in windows?
- HowTo: Set an Environment Variable in Windows — Command Line and Registry
- How to Set Environment Variable in Windows
- Environment variables to configure the AWS CLI
- Chasing Code
How to Set Environment Variable in Windows
How to create an environment variable in Windows?To Create a User Environment
Variable in Windows 10,
set
Get-ChildItem Env:
echo %[variable_name]%
echo $Env:[variable_name]
setx [variable_name] "[variable_value]"
setx Test_variable "Variable value"
setx [variable_name] "[variable_value]" /M
reg query HKEY_CURRENT_USEREnvironment
reg query "HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession ManagerEnvironment"
reg query HKEY_CURRENT_USEREnvironment /v [variable_name]
reg query "HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession ManagerEnvironment" /v [variable_name]
reg delete HKEY_CURRENT_USEREnvironment /v [variable_name] /f
reg delete "HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession ManagerEnvironment" /v [variable_name] /f
setx [variable_name] trash
How to export and import environment variables in windows?
Add a comment. 130. I would use the SET command from the command prompt to
export all the variables, rather than just PATH as recommended above. C:\> SET
>> …
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
HKEY_CURRENT_USER\Environment
C:\> SET >> allvariables.txt
C:\> for /F %A in (allvariables.txt) do SET %A
regedit /e "%userprofile%\Desktop\my_user_env_variables.reg" "HKEY_CURRENT_USER\Environment"
set TODAY=%DATE:~0,4%-%DATE:~5,2%-%DATE:~8,2%
regedit /e "%CD%\user_env_variables[%TODAY%].reg" "HKEY_CURRENT_USER\Environment"
regedit /e "%CD%\global_env_variables[%TODAY%].reg" "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
global_env_variables[2017-02-14].reg
user_env_variables[2017-02-14].reg
C:\> PATH > path.txt
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
HKEY_CURRENT_USER\Environment
@echo off
:: RegEdit can only export into a single file at a time, so create two temporary files.
regedit /e "%CD%\environment-backup1.reg" "HKEY_CURRENT_USER\Environment"
regedit /e "%CD%\environment-backup2.reg" "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
:: Concatenate into a single file and remove temporary files.
type "%CD%\environment-backup1.reg" "%CD%\environment-backup2.reg" > environment-backup.reg
del "%CD%\environment-backup1.reg"
del "%CD%\environment-backup2.reg"
gci env:* | sort-object name | Where-Object {$_.Name -like "MyApp*"} | Foreach {"[System.Environment]::SetEnvironmentVariable('$($_.Name)', '$($_.Value)', 'Machine')"}
# export_env.ps1
$Date = Get-Date
$DateStr = '{0:dd-MM-yyyy}' -f $Date
mkdir -Force $PWD\env_exports | Out-Null
regedit /e "$PWD\env_exports\user_env_variables[$DateStr].reg" "HKEY_CURRENT_USER\Environment"
regedit /e "$PWD\env_exports\global_env_variables[$DateStr].reg" "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment">>SystemEnvVariablesSourceMachine.txt
reg query "HKEY_CURRENT_USER\Environment">>UserEnvVariablesSourceMachine.txt
(?:\A\r?\n|^HKEY_CURRENT_USER\\Environment\r?\n?|^HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment\r?\n?|^\r?\n$|\r?\n\Z)
Literally Empty
^\s+(.*?)\s{4}\w+\s{4}(.*?)$
\1=\2
SystemEnvVariablesDestinationMachine.txt
UserEnvVariablesDestinationMachine.txt
SystemEnvVariablesFinalMerge.txt
UserEnvVariablesFinalMerge.txt
(^\w+=|.*?(?:;|$))
\1\n
(.)$\r?\n
\1
Get-Content .\UserEnvVariablesFinalMerge.txt | ForEach-Object {
$envVarDataSplit = $($_).split("=")
if($($envVarDataSplit).count -gt 0)
{
Write-Output "Key: $($envVarDataSplit[0]) ~ Value: $($envVarDataSplit[1])"
SETX $envVarDataSplit[0] "$($envVarDataSplit[1])"
}
}
Get-Content .\SystemEnvVariablesFinalMerge.txt | ForEach-Object {
$envVarDataSplit = $($_).split("=")
if($($envVarDataSplit).count -gt 0)
{
Write-Output "Key: $($envVarDataSplit[0]) ~ Value: $($envVarDataSplit[1])"
SETX $envVarDataSplit[0] "$($envVarDataSplit[1])" /M
}
}
How to export and import environment variables in windows?
The first set are system/global environment variables; the second set are
user-level variables. Edit as needed and then import the .reg files on the new
machine. Solution 2. …
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
HKEY_CURRENT_USER\Environment
C:\> SET >> allvariables.txt
C:\> for /F %A in (allvariables.txt) do SET %A
regedit /e "%userprofile%\Desktop\my_user_env_variables.reg" "HKEY_CURRENT_USER\Environment"
set TODAY=%DATE:~0,4%-%DATE:~5,2%-%DATE:~8,2%
regedit /e "%CD%\user_env_variables[%TODAY%].reg" "HKEY_CURRENT_USER\Environment"
regedit /e "%CD%\global_env_variables[%TODAY%].reg" "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
global_env_variables[2017-02-14].reg
user_env_variables[2017-02-14].reg
C:\> PATH > path.txt
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
HKEY_CURRENT_USER\Environment
How to export and import environment variables in windows?
The following will dump the PATH environment variable to a file named
path.txt. C:\> PATH > path.txt Registry Method. The Windows Registry holds all
the environment …
gci env:* | sort-object name | Where-Object {$_.Name -like "MyApp*"} | Foreach {"[System.Environment]::SetEnvironmentVariable('$($_.Name)', '$($_.Value)', 'Machine')"}
@echo off
:: RegEdit can only export into a single file at a time, so create two temporary files.
regedit /e "%CD%\environment-backup1.reg" "HKEY_CURRENT_USER\Environment"
regedit /e "%CD%\environment-backup2.reg" "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
:: Concatenate into a single file and remove temporary files.
type "%CD%\environment-backup1.reg" "%CD%\environment-backup2.reg" > environment-backup.reg
del "%CD%\environment-backup1.reg"
del "%CD%\environment-backup2.reg"
# export_env.ps1
$Date = Get-Date
$DateStr = '{0:dd-MM-yyyy}' -f $Date
mkdir -Force $PWD\env_exports | Out-Null
regedit /e "$PWD\env_exports\user_env_variables[$DateStr].reg" "HKEY_CURRENT_USER\Environment"
regedit /e "$PWD\env_exports\global_env_variables[$DateStr].reg" "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
set TODAY=%DATE:~0,4%-%DATE:~5,2%-%DATE:~8,2%
regedit /e "%CD%\user_env_variables[%TODAY%].reg" "HKEY_CURRENT_USER\Environment"
regedit /e "%CD%\global_env_variables[%TODAY%].reg" "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
global_env_variables[2017-02-14].reg
user_env_variables[2017-02-14].reg
regedit /e "%userprofile%\Desktop\my_user_env_variables.reg" "HKEY_CURRENT_USER\Environment"
C:\> SET >> allvariables.txt
C:\> for /F %A in (allvariables.txt) do SET %A
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
HKEY_CURRENT_USER\Environment
C:\> PATH > path.txt
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
HKEY_CURRENT_USER\Environment
HowTo: Set an Environment Variable in Windows — Command Line and Registry
To set persistent environment variables at the command line, we will use
setx.exe. It became part of Windows as of Vista/Windows Server 2008. Prior to
that, it was …
1
set
1
2
3
4
5
6
ALLUSERSPROFILE=C:\ProgramData
APPDATA=C:\Users\user\AppData\Roaming
.
.
.
windir=C:\Windows
1
echo %ProgramFiles%
1
C:\Program Files
1
Get-ChildItem Env:
1
2
3
4
5
6
7
8
Name Value
---- -----
ALLUSERSPROFILE C:\ProgramData
APPDATA C:\Users\user\AppData\Roaming
.
.
.
windir C:\Windows
1
echo $Env:ProgramFiles
1
C:\Program Files
1
setx EC2_CERT "%USERPROFILE%\aws\cert.pem"
1
echo %EC2_CERT%
1
C:\Users\user\aws\cert.pem
1
setx EC2_HOME "%APPDATA%\aws\ec2-api-tools" /M
1
reg query HKEY_CURRENT_USER\Environment
1
2
3
HKEY_CURRENT_USER\Environment
TEMP REG_EXPAND_SZ %USERPROFILE%\AppData\Local\Temp
TMP REG_EXPAND_SZ %USERPROFILE%\AppData\Local\Temp
1
reg query HKEY_CURRENT_USER\Environment /v TEMP
1
2
HKEY_CURRENT_USER\Environment
TEMP REG_EXPAND_SZ %USERPROFILE%\AppData\Local\Temp
1
reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
ComSpec REG_EXPAND_SZ %SystemRoot%\system32\cmd.exe
FP_NO_HOST_CHECK REG_SZ NO
NUMBER_OF_PROCESSORS REG_SZ 8
OS REG_SZ Windows_NT
Path REG_EXPAND_SZ C:\ProgramData\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\
PATHEXT REG_SZ .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
PROCESSOR_ARCHITECTURE REG_SZ AMD64
PROCESSOR_IDENTIFIER REG_SZ Intel64 Family 6 Model 60 Stepping 3, GenuineIntel
PROCESSOR_LEVEL REG_SZ 6
PROCESSOR_REVISION REG_SZ 3c03
PSModulePath REG_EXPAND_SZ %SystemRoot%\system32\WindowsPowerShell\v1.0\Modules\;C:\Program Files\Intel\
TEMP REG_EXPAND_SZ %SystemRoot%\TEMP
TMP REG_EXPAND_SZ %SystemRoot%\TEMP
USERNAME REG_SZ SYSTEM
windir REG_EXPAND_SZ %SystemRoot%
1
reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v PATH
1
2
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
PATH REG_EXPAND_SZ C:\ProgramData\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\
1
reg delete HKEY_CURRENT_USER\Environment /v USER_EXAMPLE /f
1
The operation completed successfully.
1
reg delete "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v SYSTEM_EXAMPLE /f
1
ERROR: Access is denied.
1
The operation completed successfully.
1
setx throwaway trash
1
SUCCESS: Specified value was saved.
How to Set Environment Variable in Windows
Set Environment Variable in Windows via GUI. Follow the steps to set
environment variables using the Windows GUI: 1. Press Windows + R to open the
…
set
Get-ChildItem Env:
echo %[variable_name]%
echo $Env:[variable_name]
setx [variable_name] "[variable_value]"
setx Test_variable "Variable value"
setx [variable_name] "[variable_value]" /M
reg query HKEY_CURRENT_USEREnvironment
reg query "HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession ManagerEnvironment"
reg query HKEY_CURRENT_USEREnvironment /v [variable_name]
reg query "HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession ManagerEnvironment" /v [variable_name]
reg delete HKEY_CURRENT_USEREnvironment /v [variable_name] /f
reg delete "HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession ManagerEnvironment" /v [variable_name] /f
setx [variable_name] trash
Environment variables to configure the AWS CLI
The AWS CLI supports the following environment variables. AWS_ACCESS_KEY_ID.
Specifies an AWS access key associated with an IAM user or role. If defined,
this environment …
$ export AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE
$ export AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
$ export AWS_DEFAULT_REGION=us-west-2
C:\> setx AWS_ACCESS_KEY_ID AKIAIOSFODNN7EXAMPLE
C:\> setx AWS_SECRET_ACCESS_KEY wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
C:\> setx AWS_DEFAULT_REGION us-west-2
C:\> set AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE
C:\> set AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
C:\> set AWS_DEFAULT_REGION=us-west-2
PS C:\> $Env:AWS_ACCESS_KEY_ID="AKIAIOSFODNN7EXAMPLE"
PS C:\> $Env:AWS_SECRET_ACCESS_KEY="wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
PS C:\> $Env:AWS_DEFAULT_REGION="us-west-2"
aws_cli_auto_prompt=on
aws_cli_auto_prompt=on-partial
Chasing Code
The Electron app would read the DEMO variable with process.env.DEMO. Simple,
right? Not so fast. It turns out you can’t use this syntax to export
environment …
'export' is not recognized as an internal or external command, operable program or batch file.
{
"name": "...",
"productName": "...",
"version": "...",
"description": "...",
"main": "...",
"scripts": {
"start": "export \"DEMO=no\" && concurrently \"npm:svelte-dev\" \"electron-forge start\"",
"start-demo": "export \"DEMO=yes\" && concurrently \"npm:svelte-dev\" \"electron-forge start\"",
"start-win": "set \"DEMO=no\" && concurrently \"npm:svelte-dev\" \"electron-forge start\"",
"start-win-demo": "set \"DEMO=yes\" && concurrently \"npm:svelte-dev\" \"electron-forge start\"",
},
...
}
Type in “regedit” from the Windows search function and open the Registry Editor.
There will be 2 type of environment variables,
SYSTEM VARIABLES
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
USER VARIABLES
HKEY_CURRENT_USER\Environment
So, to export right click on the Environment directory and choose on Export. You can save the file in .reg or in .txt format.
You May Also Like
I’m building a desktop app for managing SVG icon libraries called SVGX.app.
It uses Electron and Svelte, as well as Forge which is a helpful tool for creating and publishing such apps. I’m also using this template as a starter.
My plan was to offer two versions of the app: paid and demo. Notice I said «was» — I’m still debating the details. Anyway, I thought the demo would be a stripped edition of the full app, lacking certain features.
I decided that one way to accomplish this in an Electron app would be to create a couple of extra build tasks in package.json
, and then run the commands like so:
npm run start
oryarn start
— builds the full Mac versionnpm run start-demo
oryarn start-demo
— builds the demo Mac versionnpm run start-win
oryarn start-win
— builds the full Windows versionnpm run start-win-demo
oryarn start-win-demo
— builds the demo Windows version
Each of these tasks would export a DEMO
flag as an environment variable, that my app could use to conditionally «guard» features when the flag is false
.
Well, on Mac it’s simple: just add export \"DEMO=yes\"
in the script (notice the escaped quotes), and call it a day. The Electron app would read the DEMO
variable with process.env.DEMO
. Simple, right?
Not so fast. It turns out you can’t use this syntax to export environment variables in Windows (I use Git Bash for my terminal). The build process will fail with an error:
'export' is not recognized as an internal or external command, operable program or batch file.
I feel I should have known this, but I code almost exclusively on a Mac, so I never ran into this situation before. What does work is to use set \"DEMO=yes\"
instead.
So my script becomes what you see below:
{
"name": "...",
"productName": "...",
"version": "...",
"description": "...",
"main": "...",
"scripts": {
"start": "export \"DEMO=no\" && concurrently \"npm:svelte-dev\" \"electron-forge start\"",
"start-demo": "export \"DEMO=yes\" && concurrently \"npm:svelte-dev\" \"electron-forge start\"",
"start-win": "set \"DEMO=no\" && concurrently \"npm:svelte-dev\" \"electron-forge start\"",
"start-win-demo": "set \"DEMO=yes\" && concurrently \"npm:svelte-dev\" \"electron-forge start\"",
},
...
}
In summary:
- use
export
on Mac - use
set
on Windows
Liked this article? Share it on your favorite platform.
Table of contents
- What are environment variables?
- Classification of environment variables
- User and system environment variables
- Permanent and temporary environment variables
- Classification of environment variables
- How to modify environment variables?
- For macOS
- Detect current terminal
- Set temporary environment variables
- Set permanent user environment variables
- For Windows
- Set permanent variable
- Windows 10 or newer
- Other Windows
- Set temporary variable
- Command Prompt (cmd)
- PowerShell (the terminal used by default in VS Code)
- Git Bash
- Set permanent variable
- For Linux
- For macOS
- Resource
What are environment variables?
Environment variables generally refer to some parameters used in the operating system to specify the operating environment of the operating system, such as the location of the temporary folder and the location of the system folder. The system maintains a global variable table at runtime, which all system processes can access. Simply put, environment variables contain the information that the system and user applications will use, and are the parameters required for the system and applications to start and run.
Classification of environment variables
User and system environment variables
Modern operating systems are almost always multi-user operating systems. Different users can have different environment variables, and system environment variables are common to all users. When used, the system will append the user environment variable to the system environment variable, and then pass it to the software for use.
According to the maximum number of users allowed to operate the computer at the same time, the operating system can be divided into the single-user operating system and multi-user operating system. Allowing multiple users to use the computer at the same time is called a multi-user operating system; on the contrary, allowing at most one user to operate the computer at the same time is called a single-user operating system.
Permanent and temporary environment variables
- Permanent environment variables refer to environment variables that will not disappear due to software shutdown, power failure, or shutdown. These variables are typically stored in system configuration files.
- Temporary environment variables refer to the environment variables that exist during the running of the software. These environment variables are independent of each other and disappear when the software is closed.
How to modify environment variables?
Since the startup and operation of the system depend on environment variables, all operations need to be cautious! If the settings are wrong, it can seriously lead to downtime or even the worst case of not being able to boot the system. So make sure you know what you are doing before modifying environment variables.
For macOS
Detect current terminal
macOS has a variety of built-in shells. Before adding environment variables, you must first know what shell you are using. Open a terminal:
$ echo $SHELL
# Print the terminal you are running now
$ cat /etc/shells
# List all terminal paths. Common possible outputs:
/bin/sh
/bin/bash
/bin/zsh
/bin/sh
: The Bourne Shell (sh) is the original UNIX shell and is available on every UNIX. Bourne Shell is quite good at shell programming convenience, but it is not as good at handling user interaction as other shells./bin/bash
: Bourne Again Shell (bash) is the default shell of Linux, it is an extension of Bourne Shell. It is fully compatible with Bourne Shell and adds many features on the basis of Bourne Shell. It can provide functions such as command completion, command editing, and command history. It also includes many advantages of C Shell (csh) and Korn Shell (ksh), a flexible and powerful editing interface, and a user-friendly interface at the same time./bin/zsh
: Z shell (zsh) is a powerful terminal software that can be used both as an interactive terminal and as a script interpreter. It is compatible with bash (not compatible by default unless it is set to emulate sh) and provides many improvements, such as more efficiency, better auto-completion, better filename expansion (wildcard expansion), better Array processing, high customizability.
macOS defaults to Bourne Shell (sh). By default, the environment variable configuration files and loading order in the macOS terminal are as follows:
/etc/profile
/etc/bashrc # sh
/etc/zshrc # zsh
/etc/paths
~/.bash_profile # sh
~/.bashrc # sh
~/.zshrc # zsh
~/.bash_login
~/.profile
Where /etc/profile
, /etc/bashrc
, /etc/zshrc
and /etc/paths
are system-level environment variables, valid for all users. Their loading timing is different. Modifying them directly is not recommended.
This only guides how to add user environment variables.
Set temporary environment variables
The export
command is used to set or display environment variables. Environment variables added via export
are only valid for this login cycle. (It will disappear after exiting the terminal.)
$ export VERIABLE_NAME="value"
# Set the VARIABLE_NAME environment variable to value.
$ echo $VERIABLE_NAME
# Display the value of the VERIABLE_NAME environment variable
$ export -p
# View all current environment variables
$ env
# Another way to see all current environment variables
For example, if you wanted to append the path to the current PATH
variable, you would pass command of the form:
export PATH="$PATH:<PATH 1>:<PATH 2>:<PATH 3>:...:<PATH N>"
Set permanent user environment variables
If you want the value of a variable permanently across sessions and all Terminal windows, you must set it in the shell startup script. To add user environment variables, just modify ~/.bash_profile
(Bourne Shell) or ~/.zshrc
(zsh). These two files will run when the terminal starts. Therefore, permanent variables can be set by setting temporary environment variables in these two files. Let’s use vi
to edit files, if you’re new to vi
, or you’re looking for other ways to edit the text in the terminal, read this tutorial.
Replace <file_path>
below with ~/.bash_profile
(Bourne Shell) or ~/.zshrc
(zsh).
$ cp <file_path> <file_path>_add_var.bak
# Backup the file, if something goes wrong, you can restore it by
# $ cp <file_path>_add_var.bak <file_path>
$ vi <file_path>
# Edit the file, it will be created if it doesn't exist.
After entering the vi
editing interface, press i
to enter the editing mode. At the end of the file, add the export
command similar to set temporary environment variables. For example, if I want to add the path /usr/local/bin
to the front of PATH
, I can type export PATH="/usr/local/bin:$PATH"
. When you are done editing, press esc
, and type :wq
to save and exit the vi
editor.
In order for the modified content to take effect in the current terminal, enter the following command to make the terminal reload the file:
For Windows
Set permanent variable
Windows 10 or newer
-
Open the Start Search, type in “env”, and choose “Edit the system environment variables”:
If you are using an operating system in another language, it may not be searchable. You can press
Win
+E
to open the file explorer, right-click an empty location, select Properties to enter the System interface of the control panel, and then click Advance system settings in the list on the left. -
Click the “Environment Variables…” button.
-
Set the environment variables as needed.
- The
New
button adds an additional variable - The
Edit
button modifies the selected variable - The
Delete
button deletes the selected variable - Dismiss all of the dialogs by choosing OK. Your changes are saved!
- The
-
Next, this tutorial takes editing and adding a new path to the
PATH
environment variable as an example. Select the PATH variable in your user’s environment variable or system’s environment variable and clickEdit
. -
In the pop-up editing interface, click “New” and enter a new path. You can also click “Browse…” after clicking “New”, navigate to the path you want, and click “OK” to add a path.
-
When you are finished editing, save your changes by keep clicking OK until you close the “System Properties” window.
-
If you want the changes to take effect in a running terminal, close the terminal and rerun it.
Other Windows
For other versions of Windows, find out how to edit environment variables yourself. It is worth noting that when editing the PATH
, pay attention to the use of semicolons ;
to separate each path.
A possibly helpful article, this is the official Java help document, which mentions the specific method of setting the PATH
variable: https://www.java.com/en/download/help/path.html
Set temporary variable
Command Prompt (cmd)
$ set VERIABLE_NAME="value"
# Set the VARIABLE_NAME environment variable to value.
$ echo %VERIABLE_NAME%
# Display the value of the VERIABLE_NAME environment variable
$ set
# View all current environment variables
PowerShell (the terminal used by default in VS Code)
$ $env:VERIABLE_NAME="value"
# Set the VARIABLE_NAME environment variable to value.
$ $env:VERIABLE_NAME
# Display the value of the VERIABLE_NAME environment variable
$ dir env:
# View all current environment variables
Git Bash
$ export VERIABLE_NAME="value"
# Set the VARIABLE_NAME environment variable to value.
$ echo $VERIABLE_NAME
# Display the value of the VERIABLE_NAME environment variable
$ export -p
# View all current environment variables
$ env
# Another way to see all current environment variables
For Linux
The method is similar to that of macOS, please check the For macOS section and set it according to your personal research.
Resource
- https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_environment_variables
- https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/set_1
- https://support.apple.com/guide/terminal/use-environment-variables-apd382cc5fa-4f58-4449-b20a-41c53c006f8f/mac