Detailed explanation of the cmd batch escape character% _dos/bat

Source: Internet
Author: User
Tags constant
1,% is an escape character, usually translated into the escape character, but also has a more image of the translation of escape characters, escaping characters. That is, the% not only escapes and replaces the specific string associated with it, but is also "off" itself. and similar to the C language of the escape character "\", the double will escape and take off as a single%, four is off to double%.

2, for itself is a special command, similar to a specialized command interpreter, because its function implementation needs to execute more than one statement, so it must also have the command line (especially the command line after the function) parsing processing. When Command/cmd implements for, it naturally borrows its original command-line parsing module, so for a two-level escape feature, the statement in the for does is divided into two-level parsing and interpretation, and the first level reads in and interprets the for command line at Command/cmd. The second level usually interprets the same command line multiple times when the for read in and interprets the Do command.

We can then note that the use of command line parameter variables and environment variables in do does not require a double%, because these variables are replaced with a specific invariant string constant after the first level of escape, participating in all the execution of the For loop , while the substitution variable requires constant dynamic changes in the execution (in the Do Child command line), and this change naturally still needs to be implemented by the escape character, so the use of double% is an inevitable choice.

In addition, it is also necessary to note that the use of a for in the command line does not require a double%, which stems from the way the command interpreter handles the command line and batch processing. In earlier DOS versions,% was not considered an escape character on the command line, so it would not be escaped and removed, so it was not possible to refer directly to the environment variables at the command line. With for, you need only one% for escape and removal. In later versions of the command interpreter, support for command-line escapes was added (primarily for environment variable support), but the tradition of the command line for using single is still preserved.

In cmd, the variable delay replacement is a special case, but does not violate the above escaping principle, just for the environment variable is no longer constant.

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.