The difference between rem and :: in the bat command

The bat file is a batch file under dos. Batch files are unformatted text files that contain one or more commands. Its file extension isType the name of the batch file at the command prompt, or double-click the batch file, and the system will call cmd.exe to run the commands in the file one by one in the order in which they appear. Routine or repetitive tasks can be simplified using batch files (also known as batch programs or scripts).

 

The difference between rem and :: in the bat command

Both rem and :: function as comments, but are somewhat different.

one,

rem is a command, which is equivalent to emptying rem itself and the content behind it when running. Since it is a command, it must be on a separate line or have

A symlink like "&".

two,

When batch encounters a line starting with a colon ":" (ignoring the space before the colon), it recognizes the statement following it as a "token" rather than a command statement, so something like

Something like ":label" is just a label in a batch.

three,

For "::", the reason why it can achieve the effect of comment is because the second : is not a legal character of the label (replace it with \;. etc.), it is not used as a label

make a legal label

This is easy to understand, if you have a tag hero that will never be used in your batch, you can start it with ":hero" as a comment. only

The colon has its innate superiority - a grammatical problem.

Alright, let's go back and look at the call now. For example, "call:hero", why is there a colon? This is to differentiate from the file hero.

Four,

Well, I think you get what I mean, and I'll explain some wonderful things to you below.

Experience has taught us that using ':' in compound statements is often problematic. So when exactly does the problem arise?

Example 1,

Copy content to clipboard

Code:

@echo off

@  %%i

for %%i in (a b c) do (

: a bottle of beer

echo hero!

echo %%i

)

pause

This works fine.

Note: %%i is a unique variable parameter in the for command, which is interpreted by the for command and is only valid in the for loop.

Example 2,

Copy content to clipboard

Code:

@echo off for %%i in (a b c) do (

echo hero!

: a bottle of beer

echo %%i

)

pause

This works fine.

Example 3,

Copy content to clipboard

Code:

@echo off

for %%i in (a b c) do (

echo hero!

echo %%i

: a bottle of beer

)

pause

This is the problem.

Example 4,

Copy content to clipboard

Code:

@echo off

if a==a (

echo super!

echo hero!

: a bottle of beer

)

pause

This is also a problem.

Cases 3 and 4 both prompt -- "there should be no )" at this time. We can draw a conclusion by looking at it - the line after the label cannot be a trailing bracket ")". But very

Sorry, see example 5

Example 5,

Copy content to clipboard

Code:

@echo off

echo super!

echo hero!

: a bottle of beer

)

pause

It runs fine, it seems that the problem is also with the compound statement and not just the trailing bracket ")".

If this isn't a bug in batching, then allow me to take a wild guess. Looking back at Example 4, ":have a beer" is the last statement in the compound block.

After that, there will be no statement in the statement block. If there is a goto statement jumping over at this time, the system will not be able to identify which sentence is the next sentence, resulting in confusion.

In order to avoid this situation, the system has set such an error.

As for the case of "::", it is actually a truth. The system only knows that it is a tag and does not process it, and other "difficulties" are left to goto.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326266296&siteId=291194637