Help information:
& [...] command1 & command2 is used to separate multiple commands in a command line. Run the first command, and then run the second command.
&& [...] command1 && command2 Only when the command before the symbol && is successful, is used to run the command after the symbol. Run the first command, and then run the second command only if the first command is successful.
|| [...] command1 || command2 Only when the command before the symbol || is failed, it is used to run the command after the symbol ||. Run the first command and then run the second command only if the first command fails to run successfully (an error code greater than zero is received).
The above are all official help information (quoted for use), it is best to use Google search to download one.
Command summary:
& is generally pronounced as “and”.
Detailed explanation of parameters:
1. Talk about it first&
"Used to separate multiple commands in a command line", the official explanation actually starts from the perspective of the interpreter. From a human point of view, it should be "connecting multiple commands".
For example, I want to output 2 lines of text, hello and world, and use "&" to connect the two echo commands.
@echo off
echo hello & echo world
pause
The most common problem that "&" occurs in use is variable delay. For this issue, you can refer to the part about this knowledge point from the beginning to mastery of the batch for statement.
2. Let's talk about ErrorLevel
ErrorLevel consists of two words, one Error and the other Level. If you combine these two words, the literal meaning is actually "number of errors" or "type of errors".
If the command is executed successfully, then the "number of errors" is naturally zero. If the execution fails, then the "number of errors" is non-zero.
Posts about ErrorLevel:
The effect of batch internal commands on error level error
Some guesses about errorlevel in batch processing
3. Execute the command after the symbol successfully.
Output 2 lines of text, use "&&" to connect the two echo commands.
@echo off
echo hello && echo world
pause
The execution result is exactly the same as when linking the two commands with "&", so are the functions of "&" and "&&" the same?
The answer is: their functions are completely different, and this same result is just a coincidence.
The function of "&" is nothing more than linking multiple commands, while "&&" is to determine whether the command before the symbol is executed successfully, thereby determining whether the command after the symbol is executed.
Here is an example to illustrate:
C:\Users\helloworld\Desktop>start "" "" && echo hello
The system cannot find the file.
C:\Users\helloworld\Desktop>start "" "" & echo hello
The system cannot find the file.
hello
Use the start command to open a file that does not exist. Since the file does not exist, the execution of the start command fails. At this time, the command "echo hello" linked to be used to execute it, but the "&" link is executed.
The characteristic of "&&" makes it a special conditional operator.
4. If the command before the symbol fails, the command after the symbol is executed
The negative operation of && is ||.
For example:
C:\Users\helloworld\Desktop>start "" "" || echo hello
The system cannot find the file.
hello
The above is the entire content of this article, I hope you like it.