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

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

问题描述

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

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 命令输出的错误是由于给变量valuevalue 赋值时引号使用错误引起的代码>.

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.

有关将字符串正确分配给环境变量的更多详细信息,请阅读 为什么没有带有 '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 资源管理器.

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天全站免登陆