[Reprint] under what circumstances should variable latency be used?

Source: Internet
Author: User
Source: China dos Alliance

For latency extension of environment variables, use set /? You can see some of the instructions, but considering its poor translation level, it is recommended that you switch chcp 437 to English to view the original English instructions before viewing. Given that this article has been very detailed, and there are several Code Examples are not difficult to understand. Only a few supplements are provided here.


In many official documents that are visible, a pair of percent signs are used to close the environment variable to replace its value. This is called an extension )", this is actually the first concept.
From the perspective of the command interpreter, we can regard it as a reference, call, or get ).


The command interpreter expands the environment variables as follows: first, it reads a complete statement from the command line. After some preliminary preprocessing, before the command is interpreted and executed, it will be closed with a percent sign
String Matching. If the environment variable matching the string is found in the environment space, replace the original string and the percent sign with its value. If no match is obtained, replace it with an empty string.
Is the "extension" of the environment variable, which still belongs to the pre-processing scope of the command line.

A "complete statement ", in the command interpreter cmd of NT, it is interpreted as a composite statement that contains statement blocks, such as "for if else", and a composite statement connected with "& |&| |.


Therefore, when cmd reads the for statement, all the statements that are closed with a circular extension are read together and necessary preprocessing is completed. This includes environment variable extension, so in
Before all statements are executed, all environment variables have been replaced with the values set before for, and thus become a String constant instead of a variable. No matter how environment variables are modified in,
Only the space of environment variables is affected, not the for statement.

In order to be able to perceive the dynamic changes of environment variables within the for statement, CMD designed the extended latency environment variable feature. That is to say, after cmd reads a complete statement, it does not immediately execute the extension behavior of the variable, but will expand before a single statement is executed, that is, the extension behavior is "delayed.

The extended feature of delayed environment variables is disabled by default in cmd. Currently, there are two ways to enable it: CMD/V: Off (which is incorrect here. It should be CMD/V:On-- Namejm Note ),
It opens a new shell of the command line. before exiting the shell by using exit, the extension feature is always valid and is often used in the command line environment. The second is setlocal.
Enabledelayedexpansion, which limits the modification of environment variables to local space. After endlocal, the Extended features will be the same as the previous modifications to environment variables.
Disappears and is often used in batch processing statements.

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.