set 命令是一个在许多操作系统(如 Windows 和 Unix/Linux)中用于管理环境变量和 shell 变量的命令。通过这个命令,你可以查看、设置或取消设置变量。下面是关于 set 命令的一些详细信息:
set
命令是一个在许多操作系统(如 Windows 和 Unix/Linux)中用于管理环境变量和 shell 变量的命令。通过这个命令,你可以查看、设置或取消设置变量。下面是关于 set
命令的一些详细信息:
在 Windows 命令行中的 set
命令:
-
查看当前环境变量:
运行
set
命令可以查看当前所有的环境变量和它们的值。bashCopy Codeset
-
设置环境变量:
使用
set
命令设置或更新环境变量。语法如下:bashCopy Codeset VARIABLE_NAME=VALUE
例如,设置一个名为
PATH
的环境变量的示例:bashCopy Codeset PATH=C:\Program Files\NewPath;%PATH%
这会将
C:\Program Files\NewPath
添加到现有的PATH
环境变量中。 -
取消设置环境变量:
如果你想取消设置一个环境变量,可以使用
set
命令,不带等号和值。bashCopy Codeset VARIABLE_NAME=
例如,要取消设置
MY_VARIABLE
变量:bashCopy Codeset MY_VARIABLE=
在 Unix/Linux 系统中的 set
命令:
-
查看当前 shell 变量:
在 Unix/Linux 系统中,
set
命令主要用于查看当前 shell 中的所有变量和它们的值。bashCopy Codeset
-
设置 shell 选项:
set
命令也可以用于设置当前 shell 的行为。通过传递不同的选项,你可以更改 shell 的配置。常见的选项包括:-e
:在脚本中任何一个命令失败时立即退出。-u
:当尝试使用未定义的变量时,引发错误。-x
:在执行脚本时,打印出每一行执行的命令。
-
设置 shell 变量:
在 Unix/Linux 系统中,你可以使用
export
命令设置环境变量,但如果仅需要在当前 shell 会话中设置一个变量,你可以直接使用VARIABLE_NAME=VALUE
的语法:bashCopy CodeMY_VARIABLE=HelloWorld
set
命令非常有用,可以帮助你查看和管理系统的环境变量以及 shell 的配置。
set /?
set /? SET [variable=[string]] variable Specifies the environment-variable name. Type SET without parameters to display the current environment variables. If Command Extensions are enabled SET changes as follows: SET command invoked with just a variable name, no equal sign or value SET P would display all variables that begin with the letter 'P' SET command will set the ERRORLEVEL to 1 if the variable name is not SET command will not allow an equal sign to be part of the name of Two new switches have been added to the SET command: SET /A expression The /A switch specifies that the string to the right of the equal sign () - grouping If you use any of the logical or modulus operators, you will need to The /P switch allows you to set the value of a variable to a line of input Environment variable substitution has been enhanced as follows: %PATH:str1=str2% would expand the PATH environment variable, substituting each occurrence May also specify substrings for an expansion. %PATH:~10,5% would expand the PATH environment variable, and then use only the 5 %PATH:~-10% would extract the last 10 characters of the PATH variable. %PATH:~0,-2% would extract all but the last 2 characters of the PATH variable. Finally, support for delayed environment variable expansion has been Delayed environment variable expansion is useful for getting around set VAR=before would never display the message, since the %VAR% in BOTH IF statements set LIST= in that it will NOT build up a list of files in the current directory, for %i in (*) do set LIST= %i which just keeps setting LIST to the last file found. Delayed environment variable expansion allows you to use a different set VAR=before set LIST= If Command Extensions are enabled, then there are several dynamic %CD% - expands to the current directory string. %DATE% - expands to current date using same format as DATE command. %TIME% - expands to current time using same format as TIME command. %RANDOM% - expands to a random decimal number between 0 and 32767. %ERRORLEVEL% - expands to the current ERRORLEVEL value %CMDEXTVERSION% - expands to the current Command Processor Extensions %CMDCMDLINE% - expands to the original command line that invoked the %HIGHESTNUMANODENUMBER% - expands to the highest NUMA node number |