What is latency environment variable extension?

Source: Internet
Author: User

the full name of the latency variable \ "extended environment variable \". To understand this, we must first understand What extension is!
when cmd explains our commands, it first reads a complete command from the command line and then matches the command format, check whether the command format you entered meets the requirements.
if we want to reference some variables in our command, how can we let cmd identify the variable while interpreting our command? In this case, you can add a "%" sign on both sides of the variable name.
for example, % name % . when cmd matches the format of the command to read our entire line, it will find that the name character is added with the % sign on both sides, it will not treat it as a common character, the variable name is name! Then, CMD finds the value corresponding to the variable name and replaces the variable name with the value of the variable name. (If the variable name does not exist, return NULL
value ). then, replace and execute the matched command! The process of replacing the value is called variable extension. To put it bluntly, the variable name is replaced with its value and then executed! That is, how to identify a
variable in batch processing.
(Note: This is only the extension of variables, not the extension of delayed environment variables. To understand the extension of delayed environment variables, you must first understand the extension of variables) that is, how to identify a variable in batch processing.

For example, this bat
Set Var = test
Echo % var %
CMD reads echo
After the % var % command, the matching operation will be performed, and it will immediately find that there are % signs on both sides of the VaR character, then he will treat it as a variable, check whether the VaR variable name has a value.
Replace the variable name var with its value. Here, our VaR is stored in the previous command set
In Var = test, assign the value of VaR to test, so he will replace the variable name % var % with test. The replaced result is Echo.
Test. All these steps are the matching steps in cmd. After the matching is completed, the echo test statement is executed. In this case, a test is echo in cmd.

What is environment variable extension? What is delay environment variable extension?
When we understand the environment variable extension, we know that when cmd explains the command, it first reads a complete command and then performs the matching operation, when matching, He will replace the variable in the command with the variable value, and then execute the replaced command.
The problem lies in \ "a complete command \". In bat, if for and other commands can all be enclosed in brackets to nest some commands for execution. in this case, the complete format of a command that can add an extension code to embed other commands is for % I in (....) such a whole. at this point, if we embed some commands for setting variable values in parentheses, the problem will occur!

Look at the example
@ Echo off
For/L % I in (1, 1, 5) Do (
Set Var = % I
Echo % var %
)
The error message for five blank rows is displayed after execution! Why? Based on the knowledge above

Through these two examples, we should have understood that if only environment variables expand this process, if we execute the value assignment operation in commands that can be nested, this will cause the problem of assigning values to variables in bat, that isThe variable is replaced before the command is executed. As a result, no modifications to the variable in the subsequent operation cannot be reflected in the current batch..

At this time, the concept of \ "extension of delayed environment variables \" is proposed, namely, extension of delayed environment variables.Each Command replaces the variable before execution to ensure that any modifications to the variable can take effect in real time.
 In batch processing, we can use the setloacl enabledelayedexpansion command to enable \ "Deferred environment variable extension \"

After \ "extended environment variable \" is enabled, when cmd interprets commands with nested formats, it first executes the nested commands one by one, then perform the matching operation.
The value assignment operation will be completed. After \ "extended environment variable \" is enabled, CMD will use it! To determine whether the variable is a variable. If the variable is not enabled, the variable is determined in the format of % name %.
Use it! Name! The format is determined. we need to pay attention to this symbol!

Example:
@ Echo off
Setlocal enabledelayedexpansion
Set Var = 1
For/L % I in (1, 1, 5) Do (
Set/a var + = % I
Echo! VaR!
)
Pause
In this case, you should understand what is delayed environment variable extension.

Another example
@ Echo off
Set Var = Test & Echo % test %
Pause
This command is placed on one line, indicating that it is a complete command. If \ "extended environment variable \" is not enabled, the above assignment error will occur!
Change to this
@ Echo off
Setlocal enabledelayedexpansion
Set Var = Test & Echo! VaR!
Pause.

@ echo off
setlocal enabledelayedexpansion
If "% 1" = "" (
echo "AAA"
) else (
set hour = % 1
set/A smallhour = hour-1
echo! Hour!
echo! Smallhour!
If "% time :~ 0, 5% "geq" % smallhour %: 45 "if" % time :~ 0, 5% "Leq" % hour %: 15 "Echo" AAA "
)

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.