如何用空格设置环境变量? [英] How to set environment variables with spaces?

查看:39
本文介绍了如何用空格设置环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用批处理文件将值设置为环境变量.我为此编写了脚本:

I need to set values to a environmental variable using a batch file. I wrote the script for this:

@echo off
set value="Hello world"
setx -M srijani "%srijani%;%value%"

它给出了错误:

ERROR: Invalid syntax. Default option is not allowed more than '2' time(s).
Type "SETX /?" for usage.

我搜索了一下,发现在使用空格时,我们需要将其写在双引号内.

I googled and found that while using white spaces we need to write it inside a double quotes.

set value="Hello world"

但是,那也行不通.

注意:我使用的是 Windows 7.

Note: I am on Windows 7.

推荐答案

setx命令输出的错误是由于在将字符串赋值给变量value.

The error output by command setx is caused by wrong usage of the quotes on assigning the string to variable value.

命令为set,参数为variable=value.对于大多数命令和应用程序,如果参数包含 1 个或多个空格或此列表中的任何其他字符,则可以且通常需要用双引号括起来:&()[]{}^=;!'+,`~.通过在命令提示符窗口 cmd/?help cmd 中运行,这些字符会显示在最后的帮助页面输出中.

The command is set and the parameter is variable=value. As for most commands and applications it is possible and often required to surround a parameter with double quotes if containing 1 or more spaces or any other character from this list: &()[]{}^=;!'+,`~. Those characters are displayed on last help page output by running in a command prompt window cmd /? or help cmd.

但这里是错误的:

set value="Hello world"

等号后的第一个双引号,命令set的整个参数variable=value不包含在双引号中.

With first double quote after the equal sign the entire parameter variable=value of command set is not enclosed in double quotes.

这导致将双引号解释为字符串的一部分以分配给名称为 value 的变量.从等号到行尾的所有内容,包括双引号和可能存在的尾随空格和水平制表符,都在这里分配给变量 value 而不仅仅是字符串 Hello world.

This results in interpreting the double quotes as part of the string to assign to variable with name value. Everything from the equal sign to end of line including the double quotes and possibly existing trailing spaces and horizontal tabs is assigned here to variable value instead of just the string Hello world as expected.

关于扩展线

setx -M srijani "%srijani%;%value%"

因此结果是:

setx -M srijani "Value of variable srijani;"Hello world""

并且命令 setx 将错误的引用参数解释为语法错误.

And command setx interprets the wrong quoted parameter as syntax error.

正确的是使用:

set "value=Hello world"

现在命令 set 的整个参数用双引号括起来.因此在解析行时忽略的是:

Now the entire parameter of command set is enclosed in double quotes. Therefore ignored on parsing the line are:

  • 命令 set 和第一个双引号之间的所有空格/制表符,
  • 第一个双引号,
  • 最后一个双引号,
  • 以及最后一个双引号后可能存在的所有空格/制表符.
  • all spaces/tabs between command set and the first double quote,
  • the first double quote,
  • the last double quote,
  • and all perhaps existing spaces/tabs after last double quote.

因此,只需将 Hello world 分配给名为 value 的变量.

So just Hello world is assigned to a variable with name value.

有关将字符串正确分配给环境变量的更多详细信息,请阅读 Why is no string output with 'echo %var% 上的答案' 在命令行上使用 'set var = text' 后? 它还包含一个简单的演示批处理代码.

For more details about correct assignment of a string to an environment variable read answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line? It contains also a simple demo batch code.

更多信息:

如何解释在中间某处包含 1 个或多个引号的参数字符串取决于命令和应用程序.如 的参数的行为可能因使用的编译器而异/3074564">批处理文件:列出特定文件夹中的rar文件并将结果写入文本文件,当然还有命令/应用程序的源代码.

How an argument string containing 1 or more quotes somewhere in the middle is interpreted depends on command respectively application. The behavior on interpreting an argument with 1 or more " within an argument string can vary depending on used compiler as explained in an answer on batch file: list rar file in specific folder and write result into text file and of course the source code of the command / application.

对于大多数命令和应用程序,正确的语法是:

For most commands and applications the correct syntax is:

command "parameter in quotes"
"Path to applicationapp.exe" "parameter in quotes" 

但有些应用程序需要在参数字符串中间加上引号.Windows Explorer 就是此类应用程序的一个示例.

But there are applications which require quotes in the middle of an argument string. An example of such an application is Windows Explorer.

从批处理文件中打开 Explorer 窗口需要以下语法,当前目录显示在窗口中.

The following syntax is required to open an Explorer window from within a batch file with current directory displayed in window.

explorer.exe /e,"%CD%"

不工作的是:

explorer.exe "/e,%CD%"
explorer.exe /e "%CD%"

所以 explorer.exe 要求在 /e 之后指定要打开的目录, 在参数字符串中间加上引号,否则它会解释 "/e,%CD%" 分别为 "/e %CD%" 作为目录名称,并在 Explorer 窗口中显示路径.

So explorer.exe requires that the directory to open is specified after /e, with quotes in the middle of parameter string or it interprets "/e,%CD%" respectively "/e %CD%" as name of the directory with path to display in Explorer window.

另请参阅SS64 - Windows 资源管理器命令行选项.

这篇关于如何用空格设置环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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