Windows environment variable display, set, or delete operation details

Source: Internet
Author: User

Displays, sets, or removes Cmd.exe environment variables.

SET [variable=[string]]

variable specifies the environment variable name.
String to specify a series of strings to assign to the variable.

To display the current environment variable, type SET without parameters.

If the command extension is enabled, the SET will change as follows:

Only one variable can be used to activate the SET command, and the equals sign or value does not display all the values of all variables that match the name used by the SET command.

Example: SET P

All variables that begin with the letter P are displayed.

If the variable name is not found in the current environment, the SET command sets the ERRORLEVEL to 1.

The SET command does not allow the variable name to contain an equal sign.

Two new command-line switches were added to the SET command:

set/a expression
set/p Variable=[promptstring]

The/a command-line switch specifies that the string to the right of the equal sign is the evaluated numeric expression. The expression evaluator is simple and supports the following operations in descending order of precedence:

()-Group
! ~--Unary operator
*/%-arithmetic operator
+--Arithmetic operator
<< >>-Logical shift
&-Bitwise "and"
^-Bitwise "XOR"
| -Bitwise "OR"
= *=/=%= + =-Assignment
&= ^= |= <<= >>=
,-expression delimiter

If you use any logical or take-away operator, you need to enclose the expression string in quotation marks. Any non-numeric string key in an expression as an environment variable
Names, the values of these environment variable names are converted to numbers before they are used. If an environment variable name is specified but not defined in the current environment, the value will be set to
Zero. This allows you to use environment variable values to do calculations without typing those% symbols to get their values. If set/a is executed at the command line outside of the command script,
Then it displays the last value of the expression. The assigned operator requires an environment variable name on the left side of the assigned operator. In addition to hex with 0x prefix, octal
With 0 prefixes, the numeric value is a 10-digit number. Therefore, 0x12 is the same as 18 and 022. Note that the octal formula may be easy to confuse: 08 and 09 are invalid numbers,
Because 8 and 9 are not valid octal digits. (&)

The/P command-line switch allows you to set the value of a variable to one line input by the user. Displays the specified promptstring before the input line is read. The promptstring can be empty.

The environment variable substitution has been enhanced as follows:

%path:str1=str2%

Expands the PATH environment variable, substituting "str2" for Each "str1" in the extended result. To effectively remove all "str1" from the extended result, "str2" can be empty.
"STR1" can start with an asterisk, in which case the "str1" will always match from the beginning of the extended result to the first occurrence of the remainder of the str1.

You can also specify substrings for the extension.

%path:~10,5%

Expands the PATH environment variable, and then uses only five characters starting with the 11th (offset 10) character in the extended result. If no length is specified, the default
Value, which is the remainder of the value of the variable. If two digits (offset and length) are negative, the number used is the environment variable's numeric length plus the specified offset or length.

%path:~-10%

The last 10 characters of the PATH variable are extracted.

%path:~0,-2%

All characters of the PATH variable are extracted, except for the last two.

Finally, added support for deferred environment variable expansion. This support is always disabled by default, but it can also be used with CMD. EXE's/v command-line switch and is enabled/deactivated.
See CMD/?

Delaying the expansion of the environment variable is useful, not a time to execute, given the current expansion limit encountered when reading a line of text. The following examples show direct
Problem with variable expansion:

Set Var=before
If "%var%" = = "Before" (
Set Var=after
If "%var%" = = "after" @echo if you see this, it worked
)

The message is not displayed because the%var% in the BOTH if statement is replaced when the first if statement is read: It contains the If style, if is a
Compound statement. Therefore, IF the compound statement is actually comparing "before" and "after", the two will never be equal. Similarly, the following example will not achieve
Expected effect:

Set list=
For%i in (*) do set list=%list%%i
Echo%list%

The reason is that it does not create a list of files in the current directory, but simply sets the list variable to the last file found. This is also because%list% in
The For statement is only extended once when it is read, and the LIST variable at that time is empty. Therefore, the for loop that we really execute is:

For%i in (*) do set list=%i

This loop continues to set the LIST to the last file found.

Deferred environment variable expansion allows you to use a different character (exclamation point) to extend the environment variable at execution time. If the deferred variable extension is enabled, you can add the above
The example is written as shown below to achieve the desired effect:

Set Var=before
If "%var%" = = "Before" (
Set Var=after
If "! var! "= =" after "@echo If you see this, it worked
)

Set list=
For%i in (*) do set list=! list! %i
Echo%list%

If the command extension is enabled, there are several dynamic environment variables that can be extended but do not appear in the SET display
In the volume list. Each time the variable value is expanded, the values of these variables are calculated dynamically. If the user uses these
Any explicitly defined variable in the name that replaces the dynamic definition described below:

%cd%-Expands to the current directory string.

%date%-Expands to the current date with the same format as the date command.

%time%-Expands to the current time with the same format as the date command.

%random%-Expands to any decimal number between 0 and 32767.

%errorlevel%-Expands to the current ERRORLEVEL value.

%cmdextversion%-Expands to the current command processor extension version number.

%cmdcmdline%-Expands to the original command line that invokes the command processor.

%highestnumanodenumber%-Expands to the highest NUMA node number on this computer.

Windows environment variable display, set, or delete operation details

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.