将值/字符串分配给环境变量
最好使用以下语法并启用命令扩展来定义或修改环境变量:
set "var=text"
命令是set参数是"variable=value"
.
只要默认启用命令扩展,参数字符串就可以像所有命令一样用双引号引起来。
如果周围没有使用双引号variable=value
, 命令set将第一个等号之后的所有内容解释为行尾,包括行尾不可见的空格和水平制表符作为要分配给变量的字符串值。
变量的名称以第一个非空白字符开头(如果使用双引号,则在双引号之后),并以左到第一个等号结束。分配给变量的值从第一个等号右侧开始,到行尾或最后一个双引号结束。
set VAR = TEXT
The command line above creates an environment variable with name VARSpace and assigns the string SpaceTEXT to this variable.
的用法
set var="text"
通常不正确,因为这会导致分配给变量var
包含引号以及所有尾随空格和制表符的文本。现在参考var
在另一条带有引号的代码行上通常会导致错误消息,因为变量本身保存着已经带有引号的文本。欲了解更多详情,请参阅答案如何设置带空格的环境变量?
Example:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "var=text"
set "var = text"
set "var1=and more text"
set var2="and more text"
set var3="text with 1 trailing space"
set var
echo var3=%var3%^<- Do you see the trailing space?
echo/
set UnwantedSpaceVar=Hello
echo %UnwantedSpaceVar%! ^<- Where does the space come from?
echo/
echo There is a trailing space after Hello in this code snippet.
echo/
set "TrailingSpacesIgnored=Hi"
echo %TrailingSpacesIgnored%! ^<- The 3 trailing spaces above are ignored.
echo/
endlocal
pause
运行这个小批量代码会产生以下输出:
var=text
var = text
var1=and more text
var2="and more text"
var3="text with 1 trailing space"
var3="text with 1 trailing space" <- Do you see the trailing space?
Hello ! <- Where does the space come from?
There is a trailing space after Hello in this code snippet.
Hi! <- The 3 trailing spaces above are ignored.
封闭variable=value
即使文本本身包含 1 个或多个双引号,也可以使用引号。
set "Quote=""
这一行定义了变量Quote
与价值"
。命令set将第一个等号到最后一个双引号之后的所有内容解释为分配给名称位于第一个引号和第一个等号之间的变量的值。
Note:一个字符串值"
里面和下一个&
or &&
or ||
甚至可以使用set "variable=value"
被误解并因此导致意外行为,因为可以在运行包含以下两行的批处理文件时看到:
@echo off
set "Variable=Value with one double quote in the middle" & echo Oh, there is something wrong here!"
Value with one double quote in the middle" & echo Oh, there is something wrong here!
是要分配给环境变量的字符串,但分配给变量只是Value with one double quote in the middle
以及之后的其余行"
中间和之后&
解释为条件运算符,而不是字面上解释为要执行的附加命令cmd.exe
。同样的问题存在于&&
or ||
之后"
之间有 0 个或多个空格/制表符。这个问题不是由命令引起的set。这是由 Windows 命令处理器引起的,它将行分成命令行set还有一个命令行echo with 条件执行 of the echo命令行。
禁用命令扩展的变量赋值
命令语法set "variable=value"
如果命令扩展被禁用,则无法使用setlocal DisableExtensions
在批处理文件中(或者在 Windows 注册表中,这是非常罕见的,我从未在任何 Windows 计算机上见过)。执行批处理文件时会出现语法错误。
只能使用set variable=value
禁用命令扩展,其中值还可以包含双引号,并且必须注意尾随空格/制表符,因为它们也被分配给环境变量。
在命令提示符窗口中运行cmd /?
and setlocal /?
有关命令扩展以及哪些命令受命令扩展影响的更多信息。注意:受影响命令的输出列表丢失exit如答案中所述GOTO :EOF 返回到哪里?
通过算术表达式进行变量赋值
使用命令set有选项/A
完全改变了第二个参数的解析,即后面的字符串set /A
。有选项/A
作为第一个参数,第二个字符串被解释为算术表达式因此,处理方式与将字符串值分配给环境变量完全不同。环境变量始终是字符串类型,而不是整数类型。
选项的使用/A
需要启用命令扩展,否则命令set完全忽略该行的其余部分,没有任何错误消息。
在大多数情况下,不建议仅使用算术表达式将数字分配给环境变量,即使用set /A var=1
. set "var=1"
要不就set var=1
(并且没有尾随空格)速度要快一些,因为环境变量始终是字符串类型。
In an arithmetic expression whitespaces are interpreted as delimiters for variable names, numbers and operators. For that reason the command line set /A var = 1
does not define a variable with name VARSpace with the string Space1 as set var = 1
does. set /A var = 1
defines a variable with name VAR with string value 1 after converting 1
from string (batch file contains 1
as character with hexadecimal code value 31) to integer with value 1
and back to a string with the two values 0x31 and 0x00 (string terminating null).
通过提示进行变量赋值
也可以使用命令set有选项/P
改变了解析
变量名和等号后的字符串。变量名称和等号后面的字符串被解释为要输出的提示文本,而不是分配给环境变量的字符串。
The environment variable gets assigned the string entered by prompted user (or redirected from a file or command/application), or in case of user does not enter anything before pressing RETURN or ENTER, keeps its current value respectively is still undefined if not defined before the prompt.
选项的使用/P
需要启用命令扩展,否则命令set完全忽略该行的其余部分,没有任何错误消息。
提示用户输入字符串最常用的语法是:
set /P var="Please enter something: "
命令set在这种情况下,在打印处理之前自动删除提示文本周围的双引号STDOUT(如果未重定向,则控制台窗口)。
但工作也是:
set /P "var=Please enter something: "
请阅读这个答案有关提示文本解析以及如何输出带有双引号的提示文本的更多详细信息。