如何评估 Windows 环境变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5126512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How are Windows Environment Variables evaluated?
提问by mjsr
If I have a system and user environment variable with the same name, how are they processed? Are they concatenated? Does the user variable override the system variable? And taking that into account, if I need to add something to the Path
variable, where is it more convenient to add it?
如果我有一个同名的系统和用户环境变量,它们是如何处理的?它们是串联的吗?用户变量是否覆盖系统变量?考虑到这一点,如果我需要向Path
变量添加一些东西,在哪里添加更方便?
回答by splash
I think this article should answer you question: Environment variables in Windows NT
我认为这篇文章应该可以回答你的问题:Windows NT 中的环境变量
User environment variables
User environment variables can be viewed from Control Panel as well. The user may add, delete or modify the environment variables in the User Environment Variables for User field. These variables take precedence over system environment variables. The user path is appended to the system path.
用户环境变量
用户环境变量也可以从控制面板查看。用户可以在 User Environment Variables for User 字段中添加、删除或修改环境变量。这些变量优先于系统环境变量。用户路径附加到系统路径。
回答by Simon
Everything splashsays in their answeris correct. To be absolutely clear, there is a difference between how the user path environment variable is evaluated, and the other user environment variables are evaluated. A regular user environment variable overrides completely a system one with the same name if both exist, but only for the specific user it is specified for. However, the user path variables is treated differently. It is appendedto the system path variable when evaluating, rather than completely replacing it. I believe splash states that, but they do it so concisely I think it needs spelling out.
飞溅在他们的答案中说的一切都是正确的。绝对清楚,用户路径环境变量的评估方式与其他用户环境变量的评估方式存在差异。如果两者都存在,则常规用户环境变量完全覆盖具有相同名称的系统变量,但仅针对为其指定的特定用户。但是,用户路径变量的处理方式不同。它在评估时附加到系统路径变量,而不是完全替换它。我相信飞溅指出了这一点,但他们做得如此简洁,我认为需要详细说明。
回答by JonathanDavidArndt
Everything that splashand Simonsay in their answers are correct. The idea that the user path variable is appended has been highlighted, and I believe the consequences of that difference require some additional treatment.
飞溅和西蒙在他们的答案中所说的一切都是正确的。附加用户路径变量的想法已被强调,我相信这种差异的后果需要一些额外的处理。
Path = %Path% (System) ; %Path% (User)
路径 = %Path% (系统); %Path%(用户)
When you execute an executable program (or any executable script, such as .bat
, .vbs
, etc.) you do not need to provide the fully qualified path.
当你执行一个可执行程序(或任何可执行脚本,如.bat
,.vbs
等),则无需提供完全合格的路径。
For instance, to run java
, you can type in any of these:
例如,要运行java
,您可以输入以下任何内容:
C:/Program Files (x86)/Java/jre6/bin/java -version
java.exe -version
java -version
The first example uses a fully qualified path. This will always use the version of the Java at that exact path.
第一个示例使用完全限定的路径。这将始终使用该确切路径上的 Java 版本。
The second example will go through each of the directories in the %Path%
environment variable, looking for an executable file named java.exe
. It will run the very first one that is found, and stop searching. If there are two files named java.exe
somewhere on the %Path%
, only the first one found is used.
第二个示例将遍历%Path%
环境变量中的每个目录,查找名为java.exe
. 它将运行找到的第一个,并停止搜索。如果在 上java.exe
某处命名了两个文件%Path%
,则仅使用找到的第一个文件。
The third example, like the second, will iterate over the directories listed in the %Path%
. In addition, because a file extension was not provided, a list of executable file extensions are appended to the name of the file, in the order specified in the %PATHEXT%
environment variable. If there are several files named java.com
, java.exe
, java.bat
, etc. somewhere on the %Path%
, only the first one found is used.
第三个示例与第二个示例一样,将遍历 .csv 文件中列出的目录%Path%
。此外,由于未提供文件扩展名,可执行文件扩展名列表会按照%PATHEXT%
环境变量中指定的顺序附加到文件名。如果 上某处有多个名为java.com
、java.exe
、java.bat
等的文件%Path%
,则仅使用找到的第一个文件。
You can see the list of executable path extensions on your system by creating the following batch file:
您可以通过创建以下批处理文件来查看系统上的可执行路径扩展列表:
@echo off
echo %PATHEXT%
pause
On my machine, these are:
在我的机器上,这些是:
.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY
.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY
What does all this mean?
这是什么意思呢?
In stark contrastto other environment variable, the user path does not allow you to override the system path. The exact opposite is the case. From the examples above, there are many cases where you may with to change the default version of Java. However, if there is already a Java version listed in the system path, that is the version that will ALWAYS be found first, because the path is searched in order, from left-to-right, and the user path is appended on the right-hand side, with the system path on the left.
在鲜明的对比到其他环境变量,用户路径不允许您覆盖系统路径。情况正好相反。从上面的例子来看,在很多情况下你可能会改变 Java 的默认版本。但是,如果系统路径中已经列出了 Java 版本,则该版本总是最先找到的,因为路径是按顺序搜索的,从左到右,并且用户路径附加在右侧-hand 侧,系统路径在左侧。
What can I do about it?
我该怎么办?
If you do not have access to system environment variables, you cannot override default programs on the system path by using the the user path. (In fact, it mustbe this way, or certain programs would stop working correctly, and it would open your system to tampering by malicious software. Nobody wants that.)
如果您无权访问系统环境变量,则无法使用用户路径覆盖系统路径上的默认程序。(事实上,一定是这样,否则某些程序会停止正常工作,并且会使您的系统被恶意软件篡改。没有人想要这样。)
Instead, you must use a fully qualified path if you must use a specific version.
相反,如果您必须使用特定版本,则必须使用完全限定的路径。