显示、设置或删除 cmd.exe 环境变量。 If used without parameters, set displays the current environment variable settings.
Note
此命令需要默认启用的命令扩展。
The set command can also run from the Windows Recovery Console, using different parameters. 有关详细信息,请参阅 Windows 恢复环境 (WinRE)。
Syntax
set [<variable>=[<string>]]
set [/p] <variable>=[<promptString>]
set /a <variable>=<expression>
Parameters
Parameter | Description |
---|---|
<variable> |
指定要设置或修改的环境变量。 |
<string> |
指定要与指定环境变量关联的字符串。 |
/p | 将用户输入的输入行的值 <variable> 设置为一行。 |
<promptstring> |
指定要提示用户输入的消息。 This parameter must be used with the /p parameter. |
/a | 设置为 <string> 计算的数值表达式。 |
<expression> |
指定数值表达式。 |
/? | 在命令提示符下显示帮助。 |
Remarks
If command extensions are enabled (the default) and you run set with a value, it displays all of the variables that begin with that value.
字符
<
、>
、|
&
和^
都是特殊命令 shell 字符,并且它们必须前面有转义字符()或用引号^
括起来(<string>
例如,“StringContaining&Symbol”)。 如果使用引号将包含某个特殊字符的字符串引起来,则引号将设置为环境变量值的一部分。使用环境变量来控制某些批处理文件和程序的行为,并控制 Windows 和 MS-DOS 子系统的显示和工作方式。 The set command is often used in the Autoexec.nt file to set environment variables.
If you use the set command without any parameters, the current environment settings are displayed. These settings usually include the COMSPEC and PATH environment variables, which are used to help find programs on disk. Two other environment variables used by Windows are PROMPT and DIRCMD.
如果为该变量指定值
<variable>
,则指定的<string>
值将添加到环境中,并与<variable>
<string>
该变量相关联。 如果环境中已存在变量,则新字符串值将替换旧的字符串值。If you specify only a variable and an equal sign (without
<string>
) for the set command, the<string>
value associated with the variable is cleared (as if the variable isn't there).If you use the /a parameter, the following operators are supported, in descending order of precedence:
Operator Operation performed ( )
Grouping ! ~ -
Unary * / %
Arithmetic + -
Arithmetic << >>
Logical shift &
Bitwise AND ^
位异或 = *= /= %= += -= &= ^=
= <<= >>=
,
Expression separator 如果使用逻辑(
&&
或)或||
模数(%)运算符,请将表达式字符串括在引号中。 表达式中的任何非数值字符串都被视为环境变量名称,其值在处理之前会转换为数字。 如果指定当前环境中未定义的环境变量名称,则会分配零值,这样就可以使用环境变量值执行算术,而无需使用 % 来检索值。If you run set /a from the command line outside of a command script, it displays the final value of the expression.
数值是十进制数字,除非以 0 为前缀×为十六进制数字,否则为 0。 因此,0×12 与 18 相同,这与 022 相同。
Delayed environment variable expansion support is disabled by default, but you can enable or disable it by using cmd /v.
When creating batch files, you can use set to create variables, and then use them in the same way that you would use the numbered variables %0 through %9. You can also use the variables %0 through %9 as input for set.
如果从批处理文件调用变量值,请将值用百分比符号括起来(%)。 For example, if your batch program creates an environment variable named BAUD, you can use the string associated with BAUD as a replaceable parameter by typing %baud% at the command prompt.
Examples
To set the value TEST^1 for the environment variable named testVar
, type:
set testVar=TEST^^1
The set command assigns everything that follows the equal sign (=) to the value of the variable. 因此,如果键入 set testVar=TEST^1
,将得到以下结果 testVar=TEST1
。
To set the value TEST&1 for the environment variable testVar
, type:
set testVar=TEST^&1
To set an environment variable named include so the string c:\directory is associated with it, type:
set include=c:\directory
You can then use the string c:\directory in batch files by enclosing the name include with percent signs (%). For example, you can use dir %include%
in a batch file to display the contents of the directory associated with the include environment variable. After this command is processed, the string c:\directory replaces %include%.
To use the set command in a batch program to add a new directory to the path environment variable, type:
@echo off
rem ADDPATH.BAT adds a new directory
rem to the path environment variable.
set path=%1;%path%
set
To display a list of all of the environment variables that begin with the letter p, type:
set p
若要显示当前设备上所有环境变量的列表,请键入:
set