How do I run two commands in one line in Windows CMD?

1,235,529

Solution 1

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

Solution 2

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.

Solution 3

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

Solution 4

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

Solution 5

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

Share:
1,235,529
flybywire
Author by

flybywire

Updated on February 23, 2022

Comments

  • flybywire
    flybywire about 2 years

    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?

  • Raihan
    Raihan over 10 years
    Try cmd /c "echo foo & echo bar".
  • MEMark
    MEMark over 10 years
    Works on Win 8.0 and 8.1 as well.
  • phuclv
    phuclv almost 10 years
    this is also true for csh, tcsh and many more shells. I've never seen ; before in Linux
  • Fallenreaper
    Fallenreaper almost 10 years
    @ZaLiTHkA Can we by default, just always run with && then?
  • Admin
    Admin almost 10 years
    @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
    Moshe Katz almost 10 years
    @Fallenreaper Make sure that you are aware of the practical difference between the two: See Raihan's answer below.
  • Fallenreaper
    Fallenreaper almost 10 years
    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
    infografnet over 9 years
    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
    Disillusioned over 9 years
    @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.
  • M.M
    M.M over 9 years
    @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.
  • furman87
    furman87 over 8 years
    vagrant up && vagrant ssh worked without quotation marks on Windows 10.
  • Craig M. Brandenburg
    Craig M. Brandenburg about 8 years
    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
    SSi about 8 years
    I use set A= first to make sure the variable is not set.
  • TetraDev
    TetraDev almost 8 years
    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!
  • Anthon
    Anthon over 7 years
  • Nick
    Nick over 7 years
    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
    Nicolas about 7 years
    Use ^ after the first percent to get the new value: set A=Hello & call echo %^A%
  • Mirek Długosz
    Mirek Długosz about 7 years
    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
    PyDever about 7 years
    As I was. My bad. I will edit the post immediately. Appreciate it.
  • sactiw
    sactiw about 7 years
    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!%"
  • jeb
    jeb almost 7 years
    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
    Rajan Dhanowa almost 7 years
    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
    Ivan almost 7 years
    Rajan: he means "the next command is only executed if the first command succeeds".
  • nkef
    nkef almost 7 years
    documentation link broken, new location technet.microsoft.com/en-us/library/bb490954.aspx
  • Pranav A.
    Pranav A. over 6 years
    "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.
  • bornfromanegg
    bornfromanegg over 6 years
    @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.
  • Peter Mortensen
    Peter Mortensen over 6 years
    What has this to do with running two commands in one line in Windows CMD?
  • NicklasF
    NicklasF over 5 years
    And if you as me thought PowerShell is the same as a Terminal, then semicolon ; is the way to go in PowerShell
  • AlexD
    AlexD over 5 years
    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.
  • Anu Shibin Joseph Raj
    Anu Shibin Joseph Raj almost 5 years
    Should be "findstr" instead of just "find"
  • Ivan_Bereziuk
    Ivan_Bereziuk almost 5 years
    In addition you can structure your code by using temporal variable. Consider command1 && set succeeded=Yes if succeeded EQU Yes (..)
  • TripeHound
    TripeHound over 4 years
    @AlexD Or use double-quotes to avoid stray spaces, as in set "b=3" & ....
  • GreenRaccoon23
    GreenRaccoon23 about 4 years
    Also, mixing & with if statements can be a little tricky. To run a command after an if statement which does not have an else statement, use (if condition command1) & command2. Do not try to use if condition (command1) & command2 because command2 will not run.
  • joedotnot
    joedotnot over 3 years
    (command1 & command2) does not work.. e.g. start (dir cd)
  • jeb
    jeb over 3 years
    There is a 9 years old answer, describing the same, but with explanations. Btw. && does not always execute the next command, only when the first command executes successfully
  • Alexander Samoylov
    Alexander Samoylov over 3 years
    Thanks Nick and Nicolas for you solutions! I tried to use them in more general case, when after set there are multiple commands separated with &, and I see it works onl with additional syntactical tricks. With Nick's solution I should escape the command separator as ^&: set var=Hello & cmd /v /c echo firstUsage=!var! ^& echo secondUsage=!var!. With Nicolas's solution I should repeat call before every subcommand: set var=Hello & call echo firstUsage=%^var% & call echo secondUsage=%^var% (probably one knows how to improve this).
  • zzzhhh
    zzzhhh almost 3 years
    Use start if you wanna run two applications at once. The command in the answer will not run the second app until the first one terminates. If the first one is a GUI app, you have to close it before running the second, which may not what you want.
  • djdanlib
    djdanlib almost 3 years
    Correct. The question was regarding the Linux shell's ; operator, which runs things sequentially. If you want to run things in parallel like the Linux shell's & operator, you have to use START on Windows. There's also a way to wait for your processes to exit: stackoverflow.com/a/43762349/691749 And of course, PowerShell might be appropriate for more complex scenarios anyway.
  • john v kumpf
    john v kumpf almost 2 years
    @GreenRaccoon23 everything after the condition is part of the then-clause. So if exist c:\ echo 1 & echo 2 runs both, and if not exist c:\ echo 1 & echo 2 runs neither. But your (if not exist c:\ echo 1) & echo 2 works. It runs 2 regardless.
  • john v kumpf
    john v kumpf almost 2 years
    I'm surprised no one has commented on if here, cuz it's tricky. @GreenRaccoon23 commented below on @Raihan's answer. Everything after the if condition is part of the then-clause even if it has a &. So if exist c:\ echo 1 & echo 2 runs both, and if not exist c:\ echo 1 & echo 2 runs neither. But @GreenRaccoon23's suggestion (if not exist c:\ echo 1) & echo 2 works; it runs 2 regardless.