By flybywire


2011-11-08 18:31:04 8 Comments

I want to run two commands in a Windows CMD console.

In Linux I would do it like this

touch thisfile ; ls -lstrh

How is it done on Windows?

17 comments

@هادی کشاورز Hadi Keshavarz 2019-04-19 03:29:25

Yes there is. It's &.

&& will execute command 2 when command 1 is complete providing it didn't fail.

& will execute regardless.

@Shashin Bhayani 2019-04-19 03:51:14

Check out the How to answer stackoverflow.com/help/how-to-answer

@Rajan Dhanowa 2017-06-05 15:06:44

It's simple: just differentiate them with && signs. Example:

echo "Hello World" && echo "GoodBye World".

"Goodbye World" will be printed after "Hello World".

@jeb 2017-06-07 08:04:54

Independent of the fact, that there are already old answers that shows the same, it's still not quite correct. && is a conditional operator, the next command is only executed when the first command succeded (errorlevel=0)

@Rajan Dhanowa 2017-06-07 18:19:50

of course. it's self evident if the person wanna run two commands those two will be correct and thus everything will go good

@Ivan 2017-06-23 15:44:26

Rajan: he means "the next command is only executed if the first command succeeds".

@Pranav A. 2017-08-19 23:55:28

"Goodbye World will be printed after Hello World" provided printing Hello World did not fail. as @jeb has said, && is conditional. & runs commands regardless if the previous was successful or not.

@Raihan 2011-11-08 18:36:40

A quote from the documentation:

Using multiple commands and conditional processing symbols

You can run multiple commands from a single command line or script using conditional processing symbols. When you run multiple commands with conditional processing symbols, the commands to the right of the conditional processing symbol act based upon the results of the command to the left of the conditional processing symbol.

For example, you might want to run a command only if the previous command fails. Or, you might want to run a command only if the previous command is successful.

You can use the special characters listed in the following table to pass multiple commands.

  • & [...]
    command1 & command2
    Use to separate multiple commands on one command line. Cmd.exe runs the first command, and then the second command.

  • && [...]
    command1 && command2
    Use to run the command following && only if the command preceding the symbol is successful. Cmd.exe runs the first command, and then runs the second command only if the first command completed successfully.

  • || [...]
    command1 || command2
    Use to run the command following || only if the command preceding || fails. Cmd.exe runs the first command, and then runs the second command only if the first command did not complete successfully (receives an error code greater than zero).

  • ( ) [...]
    (command1 & command2)
    Use to group or nest multiple commands.

  • ; or ,
    command1 parameter1;parameter2
    Use to separate command parameters.

@Raihan 2013-11-01 17:44:57

Try cmd /c "echo foo & echo bar".

@Robert Grant 2014-08-26 14:24:51

Yes, that worked with quotations marks!

@furman87 2015-10-29 00:12:06

vagrant up && vagrant ssh worked without quotation marks on Windows 10.

@TetraDev 2016-06-01 18:37:34

Thanks! I didn't know about singe & - makes it asynchronous and run both in parallel. I was stuck because I was using && with is synchronous and only continues if the first succeeds!

@nkef 2017-06-26 12:14:14

documentation link broken, new location technet.microsoft.com/en-us/library/bb490954.aspx

@bornfromanegg 2017-09-13 09:50:28

@TetraDev & does not make the commands asynchronous or parallel. command1 & command2 will run command2 after command1 has completed. The success or failure of command1 is irrelevant.

@Ivan_Bereziuk 2019-07-04 08:53:45

In addition you can structure your code by using temporal variable. Consider command1 && set succeeded=Yes if succeeded EQU Yes (..)

@djdanlib 2011-11-08 18:33:22

Like this on all Microsoft OSes since 2000, and still good today:

dir & echo foo

If you want the second command to execute only if the first exited successfully:

dir && echo foo

The single ampersand (&) syntax to execute multiple commands on one line goes back to Windows XP, Windows 2000, and some earlier NT versions. (4.0 at least, according to one commenter here.)

There are quite a few other points about this that you'll find scrolling down this page.

Historical data follows, for those who may find it educational.

Prior to that, the && syntax was only a feature of the shell replacement 4DOS before that feature was added to the Microsoft command interpreter.

In Windows 95, 98 and ME, you'd use the pipe character instead:

dir | echo foo

In MS-DOS 5.0 and later, through some earlier Windows and NT versions of the command interpreter, the (undocumented) command separator was character 20 (Ctrl+T) which I'll represent with ^T here.

dir ^T echo foo

@Joey 2011-11-08 18:37:03

At least down to NT 4.

@MEMark 2013-11-25 08:53:46

Works on Win 8.0 and 8.1 as well.

@ZaLiTHkA 2014-04-03 20:28:19

Just for the sake of clarity: you should note the single & symbol here is explicitly for a live command prompt. When commands are run by passing a string to be executed as a script (shortcuts, batch files, etc.. ), then double && are required.

@Fallenreaper 2014-07-16 13:28:05

@ZaLiTHkA Can we by default, just always run with && then?

@ZaLiTHkA 2014-07-16 22:50:48

@Fallenreaper, that does work in the straight cmd prompt as well, so I suppose it's not a bad habit to get into. :)

@Moshe Katz 2014-07-23 19:30:58

@Fallenreaper Make sure that you are aware of the practical difference between the two: See Raihan's answer below.

@Fallenreaper 2014-07-25 18:44:20

Got ya. A && B, B only will run if A is successful, whereas A & B will run B after A, no matter what the outcome of A is. Thanks for the heads up

@infografnet 2014-09-12 13:33:49

somehow I cannot make this working in one line: set myvar=myval && echo %myvar% . The myvar is not set immediately, or at least echo cannot fetch it. Only with separate call "echo %myvar%" it shows the correct value

@Disillusioned 2014-10-22 12:59:04

@infografnet That's a different issue. Both commands are run, but the environment variable substitutions are evaluated before either command is actually executed. So echo %myvar% will be run as echo OldValueOfMyVar. This problem can be resolved by using the Delayed Expansion feature (only available in batch files though). So try the following inside a batch file: setlocal EnableDelayedExpansion && set MyVar=MyVal && echo !MyVar! && endlocal. (NOTE: The feature requires you to use ! marks in place of the % symbols.

@Disillusioned 2014-10-22 13:00:47

For more info on my previous comment, see Raymond Chen's blog: blogs.msdn.com/b/oldnewthing/archive/2006/08/23/714650.aspx

@Aleksey Napolskih 2015-03-14 00:18:41

@CraigYoung I can not get your string to work... i.e. opening a cmd and pasting and running setlocal EnableDelayedExpansion && set MyVar=MyVal && echo !MyVar! && endlocal results in !MyVar!.... Windows 7 Pro (if that's important)... any pointers on how to fix that would be appreciated

@Disillusioned 2015-03-14 07:10:43

@AlekseyNapolskih Did you miss this part of the comment: (only available in batch files though). So try the following inside a batch file? So instead of running directly at cmd, paste the line in a batch file, and run the batch file. (Win7 Pro is where I tested it. You would have to run an extremely old version of windows for it to not work.)

@Aleksey Napolskih 2015-03-16 20:18:47

@CraigYoung thanks for your reply. I did not miss that part. I am trying to run that string from VB by initiating the cmd /k etc... so I was not sure if this is considered a batch of running from cmd.

@Disillusioned 2015-03-16 21:23:45

@AlekseyNapolskih No that wouldn't be considered a batch execution. But if you put the command in a .bat or .cmd file and call the file from cmd /k, it should work.

@Nicklas Mandrup Frederiksen 2018-08-23 12:59:54

And if you as me thought PowerShell is the same as a Terminal, then semicolon ; is the way to go in PowerShell

@AlexD 2018-12-20 16:24:10

Here's also a little gem... when using variable assignment on the same line, always put the "&' right after the value, i.e, no space or windows will assign the value plus the space (s). code Bad as variable b will contain "3 " set b=3 & echo "%b" The proper way would be: set b=3& echo "%b" code Note that using quotes to display the value, will show you exactly what you are getting.

@TripeHound 2019-10-01 13:15:53

@AlexD Or use double-quotes to avoid stray spaces, as in set "b=3" & ....

@V15I0N 2017-08-11 22:22:29

When you try to use or manipulate variables in one line beware of their content! E.g. a variable like the following

PATH=C:\Program Files (x86)\somewhere;"C:\Company\Cool Tool";%USERPROFILE%\AppData\Local\Microsoft\WindowsApps;

may lead to a lot of unhand-able trouble if you use it as %PATH%

  1. The closing parentheses terminate your group statement
  2. The double quotes don't allow you to use %PATH% to handle the parentheses problem
  3. And what will a referenced variable like %USERPROFILE% contain?

@Peter Mortensen 2017-12-19 00:56:10

What has this to do with running two commands in one line in Windows CMD?

@sambul35 2016-08-03 14:31:22

A number of processing symbols can be used when running several commands on the same line, and may lead to processing redirection in some cases, altering output in other case, or just fail. One important case is placing on the same line commands that manipulate variables.

@echo off
setlocal enabledelayedexpansion
set count=0
set "count=1" & echo %count% !count!

0 1

As you see in the above example, when commands using variables are placed on the same line, you must use delayed expansion to update your variable values. If your variable is indexed, use CALL command with %% modifiers to update its value on the same line:

set "i=5" & set "arg!i!=MyFile!i!" & call echo path!i!=%temp%\%%arg!i!%%

path5=C:\Users\UserName\AppData\Local\Temp\MyFile5

@sactiw 2017-05-10 17:10:18

given example didn't work for me but below one did: cmd /V:ON /c "set i=5 & set arg!i!=MyFile!i! & echo path!i!=%temp%\%arg!i!%"

@Mile Mijatovic 2017-05-07 14:09:00

One more example: For example, when we use the gulp build system, instead of

gulp - default > build

gulp build - build build-folder

gulp watch - start file-watch

gulp dist - build dist-folder

We can do that with one line:

cd c:\xampp\htdocs\project & gulp & gulp watch

@PyDever 2017-04-29 23:16:16

Well, you have two options: Piping, or just &:

DIR /S & START FILE.TXT

Or,

tasklist | find "notepad.exe"

Piping (|) is more for taking the output of one command, and putting it into another. And (&) is just saying run this, and that.

@Mirek Długosz 2017-04-29 23:41:31

Could you edit your post to include some explanation what does & and | do and how they differ, if at all? Right now people unfamiliar with these concepts are unable to decide for themselves which one should be used in their cases.

@PyDever 2017-05-02 06:43:26

As I was. My bad. I will edit the post immediately. Appreciate it.

@Anu Shibin Joseph Raj 2019-07-01 08:31:47

Should be "findstr" instead of just "find"

@sdcxp 2017-03-18 11:16:40

I try to have two pings in the same window, and it is a serial command on the same line. After finishing the first, run the second command.

The solution was to combine with start /b on a Windows 7 command prompt.

Start as usual, without /b, and launch in a separate window.

The command used to launch in the same line is:

start /b command1 parameters & command2 parameters

Any way, if you wish to parse the output, I don't recommend to use this. I noticed the output is scrambled between the output of the commands.

@notarealname 2016-07-22 08:56:43

No, cd / && tree && echo %time%. The time echoed is at when the first command is executed.

The piping has some issue, but it is not critical as long as people know how it works.

@manojlds 2011-11-08 18:37:57

& is the Bash equivalent for ; ( run commands) and && is the Bash equivalent of && (run commands only when the previous has not caused an error).

@phuclv 2014-05-21 10:05:22

this is also true for csh, tcsh and many more shells. I've never seen ; before in Linux

@M.M 2014-12-17 00:46:30

@LưuVĩnhPhúc in sh-style shells, ; means to run the first command, wait for it to finish, then run the second command. & means to run the first command, put it to background, and run the second command. So both programs launch simultaneously. Note that these aren't combining symbols, they are trailing symbols to the first command; you can launch a single command in background with progname & without having a second command.

@PryroTech 2016-12-13 04:41:56

In order to execute two commands at the same time, you must put an & (ampersand) symbol between the two commands. Like so:

color 0a & start chrome.exe

Cheers!

@SSi 2016-03-13 11:06:24

You can use call to overcome the problem of environment variables being evaluated too soon - e.g.

set A=Hello & call echo %A%

@Craig M. Brandenburg 2016-03-21 23:15:07

This works only if the variable is not already set, in which case the echo prints out the old value, not the new value. At least, this is what I observe on my Windows 7 box.

@SSi 2016-05-11 09:48:26

I use set A= first to make sure the variable is not set.

@Nick 2017-02-01 15:02:54

A better method is to use delayed expansion (setlocal EnableDelayedExpansion in a script or cmd /v). For example: timeout 5 && cmd /v /c echo !TIME! && echo %TIME%.

@Nicolas 2017-03-28 21:00:15

Use ^ after the first percent to get the new value: set A=Hello & call echo %^A%

@SNAFUBAR 2015-01-26 20:35:36

So, I was trying to enable the specific task of running RegAsm (register assembly) from a context menu. The issue I had was that the result would flash up and go away before I could read it. So I tried piping to Pause, which does not work when the command fails (as mentioned here Pause command not working in .bat script and here Batch file command PAUSE does not work). So I tried cmd /k but that leaves the window open for more commands (I just want to read the result). So I added a pause followed by exit to the chain, resulting in the following:

cmd /k C:\Windows\Microsoft.NET\Framework\v4.0.30319\regasm.exe "%1" /codebase \"%1\" & pause & exit

This works like a charm -- RegAsm runs on the file and shows its results, then a "Press any key to continue..." prompt is shown, then the command prompt window closes when a key is pressed.

P.S. For others who might be interested, you can use the following .reg file entries to add a dllfile association to .dll files and then a RegAsm command extension to that (notice the escaped quotes and backslashes):

[HKEY_CLASSES_ROOT\.dll]
"Content Type"="application/x-msdownload"
@="dllfile"

[HKEY_CLASSES_ROOT\dllfile]
@="Application Extension"

[HKEY_CLASSES_ROOT\dllfile\Shell\RegAsm]
@="Register Assembly"

[HKEY_CLASSES_ROOT\dllfile\Shell\RegAsm\command]
@="cmd /k C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\regasm.exe \"%1\" /codebase \"%1\" & pause & exit"

Now I have a nice right-click menu to register an assembly.

@Anthon 2016-12-23 07:51:47

@dpp.2325 2014-04-16 05:41:38

cmd /c ipconfig /all & Output.txt

This command execute command and open Output.txt file in a single command

@TroodoN-Mike 2013-11-12 06:39:22

If you want to create a cmd shortcut (for example on your desktop) add /k parameter (/k means keep, /c will close window):

cmd /k echo hello && cd c:\ && cd Windows

@scrappedcola 2011-11-08 18:34:44

You can use & to run commands one after another. Example: c:\dir & vim myFile.txt

Related Questions

Sponsored Content

37 Answered Questions

[SOLVED] How to install pip on Windows?

14 Answered Questions

[SOLVED] How do I set a variable to the output of a command in Bash?

47 Answered Questions

[SOLVED] How do I import an SQL file using the command line in MySQL?

32 Answered Questions

[SOLVED] How to create an empty file at the command line in Windows?

24 Answered Questions

[SOLVED] Is there an equivalent of 'which' on the Windows command line?

8 Answered Questions

[SOLVED] Redirect Windows cmd stdout and stderr to a single file

13 Answered Questions

[SOLVED] Windows batch files: .bat vs .cmd?

31 Answered Questions

[SOLVED] How do I parse command line arguments in Bash?

15 Answered Questions

[SOLVED] How to get a list of user accounts using the command line in MySQL?

20 Answered Questions

[SOLVED] How to count lines in a document?

Sponsored Content