包含带有空格的可执行路径的环境变量是否也包含必要的引号? [英] Should environment variables that contain a executable-path with spaces also contain the necessary quotes?

查看:261
本文介绍了包含带有空格的可执行路径的环境变量是否也包含必要的引号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在定义环境变量时(在Windows上可能有更一般的指导方针)

When defining an environment variable (on Windows for me, maybe there is a more general guideline)

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?

也就是说,应该是:

set MY_TOOL="C:\Program Files (x86)\Foobar\bin\mytool.exe"

而不是上述没有空格的版本?

instead of the above version without spaces?

注意:根据 Joeys answer ,我真的应该把这个问题缩小到我给出的例子。也就是说,环境变量包含一个单独的(可执行/批处理)工具,由用户或其他批处理脚本调用。

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.

也许空格应该有不同的转义?

Maybe the spaces should be escaped differently?

推荐答案

我会说,做没有报价,使用它们到处使用变量:

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

或者甚至:

""C:\Program Files (x86)\Foobar\bin"\my tool with spaces.exe"

我怀疑将正确解析。

这篇关于包含带有空格的可执行路径的环境变量是否也包含必要的引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆