Windows 环境变量是如何评估的?
如果我有一个同名的 System
和 User
环境变量,它们是如何处理的?它们是串联的吗? user
变量是否会覆盖 system
变量?考虑到这一点,如果我需要向 Path
变量添加一些内容,在哪里添加更方便?
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? Taking that into account, if I need to add something to the Path
variable, where is it more convenient to add it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
飞溅和西蒙<的一切/a> 说他们的答案是正确的。附加用户路径变量的想法已经被强调,我相信这种差异的后果需要一些额外的处理。
当您执行可执行程序(或任何可执行脚本,例如
.bat
、.vbs
等)时,您不需要提供完全合格的路径。例如,要运行
java
,您可以输入以下任意内容:第一个示例使用完全限定路径。这将始终使用该确切路径上的 Java 版本。
第二个示例将遍历
%Path%
环境变量中的每个目录,查找名为java.exe
的可执行文件。它将运行找到的第一个,然后停止搜索。 如果%Path%
上有两个名为java.exe
的文件,则仅使用找到的第一个文件。第三个示例,例如第二个,将迭代
%Path%
中列出的目录。此外,由于未提供文件扩展名,因此将按照%PATHEXT%
环境变量中指定的顺序将可执行文件扩展名列表附加到文件名。 如果上的某处有多个名为
,仅使用找到的第一个。java.com
、java.exe
、java.bat
等的文件%Path%您可以通过创建以下批处理文件来查看系统上的可执行路径扩展列表:
在我的计算机上,这些是:
这一切是什么意思?
与其他环境变量形成鲜明对比,用户路径不允许您覆盖系统路径。情况恰恰相反。从上面的示例中可以看出,在很多情况下您可能希望更改 Java 的默认版本。但是,如果系统路径中已经列出了 Java 版本,则始终会首先找到该版本,因为路径是按从左到右的顺序搜索的,用户路径附加在右侧侧,系统路径位于左侧。
我能做什么呢?
如果您无权访问系统环境变量,则无法使用用户路径覆盖系统路径上的默认程序。 (事实上,它必须是这样,否则某些程序将停止正常工作,并且会使您的系统容易被恶意软件篡改。没有人希望这样。)
相反,您必须使用完全限定的路径如果您必须使用特定版本。
Everything that splash and Simon say 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.
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.For instance, to run
java
, you can type in any of these:The first example uses a fully qualified path. This will always use the version of the Java at that exact path.
The second example will go through each of the directories in the
%Path%
environment variable, looking for an executable file namedjava.exe
. It will run the very first one that is found, and stop searching. If there are two files namedjava.exe
somewhere on the%Path%
, only the first one found is used.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 namedjava.com
,java.exe
,java.bat
, etc. somewhere on the%Path%
, only the first one found is used.You can see the list of executable path extensions on your system by creating the following batch file:
On my machine, these are:
What does all this mean?
In stark contrast to 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 wish 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, left-to-right, with the user path appended on the right-hand side, with the system path on the left.
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 user path. (In fact, it must be this way, or certain programs would stop working correctly, and 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.
splash 中所说的一切他们的答案 是正确的。需要明确的是,用户路径环境变量的评估方式与其他用户环境变量的评估方式之间存在差异。常规用户环境变量将完全覆盖同名的系统环境变量(如果两者都存在),但仅限于为其指定的特定用户。 但是,用户路径变量的处理方式有所不同。它在评估时附加到系统路径变量,而不是完全替换它。我相信splash指出了这一点,但他们做得如此简洁,我认为需要拼写清楚。
Everything splash says in their answer is 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 appended to 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.
我认为这篇文章应该回答您的问题:Windows NT 中的环境变量
I think this article should answer you question: Environment variables in Windows NT