Running Multiple Commands in a Single Line in Command Prompt

Posted: March 16, 2015 in Technoloy
Tags: , ,

In Unix and Linux, you can string commands so that they will be executed one after the other, like so:

cd /usr/foo/home; mkdir tester; cd tester

The same can be done in Windows Command Prompt; stringing commands can be achieved through the use of ampersands and pipes:

image

Command A & Command B

Execute “Command A,” then execute “Command B” without evaluating anything. In other words, each command will be executed one after the other.

Even if the first command fails, as in the second instance the command was used here, the succeeding commands still got executed.

 

 

image

Command A && Command B

Execute “Command A,” evaluate its exit code (or errorlevel) after running, and if it’s 0, only then execute “Command B”

In the first example above, a test folder was successfully created, which made the “cd test” command possible, which in turn made the “dir” command execute. If the “mkdir test” command fails as in the last example, both the cd and dir commands are not executed.

Command A | Command B

Execute “Command A” and redirect all its output as input of “Command B.”

image

Command A || Command B

Execute “Command A,” and if the exit code evaluates to anything but 0, only then execute “Command B.”

In the example, “world” becomes the input to the second command.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s