windows 包含带空格的可执行路径的环境变量是否也应该包含必要的引号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6730429/
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
Should environment variables that contain a executable-path with spaces also contain the necessary quotes?
提问by Martin Ba
When defining an environment variable (on Windows for me, maybe there is a more general guideline)
定义环境变量时(对我来说在 Windows 上,也许有更通用的指南)
set MY_TOOL=C:\DevTools\bin\mytool.exe
if the tool is located on a path with spaces
如果工具位于带有空格的路径上
set MY_TOOL=C:\Program Files (x86)\Foobar\bin\mytool.exe
should the environment variable already contain the necessary spaces?
环境变量是否应该已经包含必要的空格?
That is, should it read:
也就是说,它应该是:
set MY_TOOL="C:\Program Files (x86)\Foobar\bin\mytool.exe"
instead of the above version without spaces?
而不是上面没有空格的版本?
Note:In light of Joeys answer, I really should narrow this question to the examples I gave. That is, environment variables that contain one single (executable / batch) tool to be invoked by a user or by another batch script.
注意:根据Joeys 的回答,我真的应该将这个问题缩小到我给出的例子中。也就是说,包含一个(可执行/批处理)工具的环境变量将由用户或另一个批处理脚本调用。
Maybe the spaces should be escaped differently?
也许空间应该以不同的方式转义?
采纳答案by Joey
I'd say, do it without quotes and use them everywhere you use the variable:
我想说,不要使用引号,并在使用变量的任何地方使用它们:
set MY_TOOL=C:\Program Files (x86)\Foobar\bin\mytool.exe
"%MY_TOOL%" -someoption someargument somefile
Especially if you let the user set the value somewhere I guess this is the safest option, since they usually tend not to surround it with quotes rather than do so.
特别是如果您让用户在某处设置值,我想这是最安全的选项,因为他们通常不会用引号将其括起来,而不是这样做。
If there are plenty of places where you use the variable you can of course redefine:
如果有很多地方使用变量,当然可以重新定义:
set MY_TOOL="%MY_TOOL%"
which makes things more resilient for you. Optionally you could detect whether there are quotes or not and add them if not present to be totally sure.
这让事情对你更有弹性。或者,您可以检测是否有引号,如果不存在则添加它们以完全确定。
When your variable represents only a path to a directory and you want to append file names there, then the "no quotes" thing is even more important, otherwise you'd be building paths like
当你的变量只代表一个目录的路径并且你想在那里附加文件名时,那么“无引号”的事情就更重要了,否则你会构建像这样的路径
"C:\Program Files (x86)\Foobar\bin"\mytool.exe
or even:
甚至:
""C:\Program Files (x86)\Foobar\bin"\my tool with spaces.exe"
which I doubt will parse correctly.
我怀疑会正确解析。
回答by Damon
The command shell can answer your question: type C:\Pro
and hit the tab key.
命令外壳可以回答您的问题:键入C:\Pro
并按 Tab 键。
Autocomplete will leave all spaces as-is and add quotes around the filename. So, this is what is "officially" expected.
自动完成将保留所有空格并在文件名周围添加引号。所以,这就是“官方”所期望的。
(this assumes that autocomplete is turned on, I'm not sure whether the default is on or off, but most people have it on anyway, I guess)
(这假设自动完成已打开,我不确定默认设置是打开还是关闭,但我猜大多数人还是打开了它)