Batch Processing Technology insider ECHO command Introduction

Source: Internet
Author: User

As we all know, if echo is followed by an environment variable, but this variable is empty, it is equivalent to echo without any parameters, that is, whether the current echo is on or off. The solution provided by many articles or tutorials is to add a dot echo after echo, which will output blank lines. Copy codeThe Code is as follows: @ echo off
Echo % demon.tw %
: ECHO is off.
Echo. % demon.tw %

Pause as far as I know, there are at least 10 methods to output empty rows Using echo:Copy codeThe Code is as follows: @ echo off

Echo =
Echo,
Echo;

Echo +
Echo/
Echo [
Echo]

Echo:
Echo.
Echo \
Pause

These ten methods can be divided into three groups, with each group's efficiency decreasing in turn. Sadly, what those classic tutorials provide is echo in the group with the lowest efficiency.

Echo. Not only inefficient, but also easy to cause errors:Copy codeThe Code is as follows: @ echo off
Cd.> echo
Echo.
Pause

I know it's hard for you to accept, but it does.

In the first group, the =,; after echo are all delimiters in batch processing. Therefore, CMD can correctly parse the echo command and use =,; as a parameter of the echo command. Yes, you are not mistaken. separators are not used to separate commands and parameters. They are usually part of parameters. Since it is a parameter, why is it not output? That is because the echo command directly skips the first character of the parameter and outputs it from the second character, while the second character is NUL, so empty lines are output.

You may ask again, why can't empty lines be output while space is used as the separator? That is because, before the output, CMD should check whether the echo command parameter is on or off, or the parameter is null: first, skip all white spaces. If the string ends after it is skipped, if no parameter is added, the output echo is on or off. If the string is not finished, call the wcsnicmp function to determine whether the remaining string is on or off, and then modify the echo status.

Therefore, adding a lot of spaces is the same effect:Copy codeThe Code is as follows: @ echo off
Echo
Echo on
Echo
Pause

For the second and third groups, the process is not that simple. Because echo is not followed by a separator, it will be regarded as a whole after resolution, echo + echo/and so on are obviously not Internal commands. CMD will use them as external commands for search. Well, you know, searching takes a long time, and that's why they are less efficient than the first group.

Unfortunately, CMD spent a lot of effort searching, but still could not find such an external command. At this time, it will try to Fix the command to see if the command contains some characters ():


As you can see, the command line is :. \'s processing is not the same as + []/. If it is + []/, CMD will directly delete them from the command and add them to the front of the original parameter; if it is :. \ If CMD expansion is enabled, the GetFileAttributes function will be called one more time to obtain the file attributes. It will take more time to call the extension function, therefore, the efficiency of the third group is slightly lower than that of the second group.

Let's explain why echo sometimes causes errors. The file name cannot contain:. \. Theoretically, the GetFileAttributes function should return-1 (INVALID_FILE_ATTRIBUTES). However, this is not the case. I do not know whether this is a BUG in the GetFileAttributes function:

Copy codeThe Code is as follows: # include <stdio. h>
# Include <windows. h>

Int main ()
{
FILE * fp = fopen ("echo", "wb ");
Fclose (fp );
Printf ("0x % x \ n", GetFileAttributes ("echo :"));
Printf ("0x % x \ n", GetFileAttributes ("echo ."));
Printf ("0x % x \ n", GetFileAttributes ("echo /"));
Return 0;
}

If you test the above C program, you will find that echo. does not return-1.

If the GetFileAttributes function does not return-1 (usually indicating that the file does not exist) or 0 × 10 (indicating that the file is a folder), the command will remain as original and run as an external command.Copy codeThe Code is as follows: @ echo off
Cd.> echo
Echo.
Pause

'Echo. 'is not recognized as an internal or external command, operable program or batch file.Copy codeThe Code is as follows: @ echo off
Cd.> echo
Setlocal disableextensions
Echo.
Pause

The CMD extension is disabled. No problem.Copy codeThe Code is as follows: @ echo off
Md echo
Echo.
Pause

Echo is a folder rather than a file. No problem.

In most cases, you should use the first group of echo, echo; echo = for output. Their efficiency is the same as that of echo (Space, it can also be used to output on or off. It can also output empty rows when the variable is empty.

However, echo, echo; echo = cannot be output /? You can use the echo + echo/echo [echo] of the second group if you need the line at the beginning. They are less efficient, but can be output as is.

I do not recommend that you use the third group of echo: echo. echo \. If you still want to use it as in the spam tutorial, I cannot.
Author: Demon
Link: http://demon.tw/reverse/cmd-internal-echo.html

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.