How do I run two commands in one line in Windows CMD?
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:
- Source: Microsoft, Windows XP Professional Product Documentation, Command shell overview
- Also: An A-Z Index of Windows CMD commands
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
flybywire
Updated on February 23, 2022Comments
-
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 over 10 yearsTry
cmd /c "echo foo & echo bar"
. -
MEMark over 10 yearsWorks on Win 8.0 and 8.1 as well.
-
phuclv almost 10 yearsthis is also true for csh, tcsh and many more shells. I've never seen
;
before in Linux -
Fallenreaper almost 10 years@ZaLiTHkA Can we by default, just always run with
&&
then? -
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 almost 10 years@Fallenreaper Make sure that you are aware of the practical difference between the two: See Raihan's answer below.
-
Fallenreaper almost 10 yearsGot 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 over 9 yearssomehow 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 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 asecho 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 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 withprogname &
without having a second command. -
furman87 over 8 years
vagrant up && vagrant ssh
worked without quotation marks on Windows 10. -
Craig M. Brandenburg about 8 yearsThis 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 about 8 yearsI use set A= first to make sure the variable is not set.
-
TetraDev almost 8 yearsThanks! 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 over 7 years
-
Nick over 7 yearsA better method is to use delayed expansion (
setlocal EnableDelayedExpansion
in a script orcmd /v
). For example:timeout 5 && cmd /v /c echo !TIME! && echo %TIME%
. -
Nicolas about 7 yearsUse ^ after the first percent to get the new value: set A=Hello & call echo %^A%
-
Mirek Długosz about 7 yearsCould 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 about 7 yearsAs I was. My bad. I will edit the post immediately. Appreciate it.
-
sactiw about 7 yearsgiven 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 almost 7 yearsIndependent 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 almost 7 yearsof course. it's self evident if the person wanna run two commands those two will be correct and thus everything will go good
-
Ivan almost 7 yearsRajan: he means "the next command is only executed if the first command succeeds".
-
nkef almost 7 yearsdocumentation link broken, new location technet.microsoft.com/en-us/library/bb490954.aspx
-
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 over 6 years@TetraDev
&
does not make the commands asynchronous or parallel.command1 & command2
will runcommand2
aftercommand1
has completed. The success or failure ofcommand1
is irrelevant. -
Peter Mortensen over 6 yearsWhat has this to do with running two commands in one line in Windows CMD?
-
NicklasF over 5 yearsAnd if you as me thought PowerShell is the same as a Terminal, then semicolon ; is the way to go in PowerShell
-
AlexD over 5 yearsHere'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 almost 5 yearsShould be "findstr" instead of just "find"
-
Ivan_Bereziuk almost 5 yearsIn addition you can structure your code by using temporal variable. Consider
command1 && set succeeded=Yes
if succeeded EQU Yes (..)
-
TripeHound over 4 years@AlexD Or use double-quotes to avoid stray spaces, as in
set "b=3" & ...
. -
GreenRaccoon23 about 4 yearsAlso, 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 useif condition (command1) & command2
because command2 will not run. -
joedotnot over 3 years(command1 & command2) does not work.. e.g. start (dir cd)
-
jeb over 3 yearsThere 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 over 3 yearsThanks 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 almost 3 yearsUse
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 almost 3 yearsCorrect. 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 almost 2 years@GreenRaccoon23 everything after the condition is part of the then-clause. So
if exist c:\ echo 1 & echo 2
runs both, andif 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 almost 2 yearsI'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&
. Soif exist c:\ echo 1 & echo 2
runs both, andif 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.