How To Run Two Commands In One Line In Windows CMD

7 July 2022

Home » Blogs » Windows » Tips and Tricks » How To Run Two Commands In One Line In Windows CMD

You can run multiple commands in one in the (CMD).

Using conditional processing symbols, you can create multiple commands with just one or script. When you use the conditional processing symbol to execute multiple commands, the instructions to the right of the conditional processing symbol act on the results of the command to the left of the conditional processing symbol.

TOP TUTORIALS:  How to Quickly Add or Remove Network Shortcut Icons in the Navigation Pane of Windows 11 File Explorer

For example, you can run a command only if the previous command failed. Alternatively, perform the command only if the previous command was successful.

Running some commands with the character “&”

command1 & command2
echo foo & echo bar

The “&” character can separate multiple commands on one . .exe will run the first command, and then the second command.

Running some commands with the character “&&”

command1 &&command2
echo foo &&amp amp echo bar

Using the “&&” character, will execute the first command (command1) first. If the first command completes successfully, a new second command (command2) will be executed.

TOP TUTORIALS:  Iperf2 vs Iperf3 - Network Performance Measurement

Run some commands with the character “|| “

command1 II command2
echo foo || echo bar

If using the “||” character, will run the first command (command1). The second command (command2) will only be executed if the first command did not complete successfully (received an greater than zero).

Note:

Ampersands (&), pipes (|), and parentheses () are special characters that must be preceded by an escape character (^) or quotation marks when you pass them as arguments.

TOP TUTORIALS:  Easy Ways to Reset and Restart Graphics Drivers (VGA) in Windows 11

Adding the escape character “^” before the command symbol allows it to be treated as plain text. Characters that usually have this special meaning can be passed and treated like ordinary characters: & \ ^ |

Echo THIS ^& THAT
Echo Heading1 ^| heading2 ^| heading3

If a command successfully completes the operation, it returns zero exit code (0) or no exit code. For more information about exit codes.

0 Comments

Submit a Comment