- Execute multiple commands with 1 line in Windows commandline?
- 8 Answers 8
- How do I run two commands in one line in Windows CMD?
- 21 Answers 21
- How to write a multiline command?
- 4 Answers 4
- Windows commands
- Prerequisites
- Command shell overview
- Command-line reference A-Z
- Split long commands in multiple lines through Windows batch file
- 6 Answers 6
Execute multiple commands with 1 line in Windows commandline?
How can I execute multiple commands in the Windows commandline with just a single line?
So for example say I want to perform an SVN update and then copy all of the files to another location.
That doesn’t work obviously. Is there a character or delimiter like ‘;’ to perform something like this?
8 Answers 8
Yes there is. It’s & .
&& will execute command 2 when command 1 is complete providing it didn’t fail.
& will execute regardless.
If you want to execute multiple commands with 1 line, where you are starting the commands with start , for example, if you wanted to execute a command like this:
Then, you need to do it in 2 steps (one-line solution is at the end of this answer).
First, write the commands to a temporary batch file (in this case, you can use & or && ):
Note that you need to «escape» each of the «&»s (ampersands) with a «^» to allow them to be treated as ordinary characters in the echo command. Alternatively, you can create the temporary batch file with a text editor, such as Notepad.
Then, use start to start the batch file:
Note: The empty pair of double-quote marks is for the «Title» that will be shown in the title-bar of the command window that start will open. This «Title» argument is technically an optional argument to start , but it is actually required, if the command that start will run is double-quoted. For instance, in the second example:
start «» «temporary foobar.cmd»
if you leave out the empty pair of double quote marks like this:
start «temporary foobar.cmd»
then start will open a new command window, and use «temporary foobar.cmd» as the new command window «Title» , and nothing will be executed in the new command window.)
If you want start to wait for the batch file to complete (after the pause is dismissed), before start completes, then you need to add the /w switch to the start command:
You can put this all together on one line and even remove (delete) the temporary batch file ( foobar.cmd ):
Note that if you are going to delete the temporary batch file, you need to run start with the /w switch, otherwise, the temporary batch file will probably be deleted before it has a chance to run.
How do I run two commands in one line in Windows CMD?
I want to run two commands in a Windows CMD console.
In Linux I would do it like this
How is it done on Windows?
21 Answers 21
Like this on all Microsoft OSes since 2000, and still good today:
If you want the second command to execute only if the first exited successfully:
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:
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.
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.
How to write a multiline command?
How do we extend a command to the next line?
Basically whats the windows alternative for Linux’s
Here we use backslashes to extend the command onto the next lines.
What’s the equivalent for Windows?
4 Answers 4
After trying almost every key on my keyboard:
So it seems to be the ^ key.
In the Windows Command Prompt the ^ is used to escape the next character on the command line. (Like \ is used in strings.) Characters that need to be used in the command line as they are should have a ^ prefixed to them, hence that’s why it works for the newline.
For reference the characters that need escaping (if specified as command arguments and not within quotes) are: &|()
So the equivalent of your linux example would be (the More? being a prompt):
The caret character works, however the next line should not start with double quotes. e.g. this will not work:
Start next line without double quotes (not a valid example, just to illustrate)
If you came here looking for an answer to this question but not exactly the way the OP meant, ie how do you get multi-line CMD to work in a single line, I have a sort of dangerous answer for you.
Trying to use this with things that actually use piping, like say findstr is quite problematic. The same goes for dealing with else s. But if you just want a multi-line conditional command to execute directly from CMD and not via a batch file, this should do work well.
Let’s say you have something like this in a batch that you want to run directly in command prompt:
Now, you could use the line-continuation carat ( ^ ) and manually type it out like this, but warning, it’s tedious and if you mess up you can learn the joy of typing it all out again.
Well, it won’t work with just ^ thanks to escaping mechanisms inside of parentheses shrug At least not as-written. You actually would need to double up the carats like so:
Instead, you can be a dirty sneaky scripter from the wrong side of the tracks that don’t need no carats by swapping them out for a single pipe ( | ) per continuation of a loop/expression:
Windows commands
All supported versions of Windows (server and client) have a set of Win32 console commands built in.
This set of documentation describes the Windows Commands you can use to automate tasks by using scripts or scripting tools.
Prerequisites
The information that is contained in this topic applies to:
- Windows Server 2019
- Windows Server (Semi-Annual Channel)
- Windows Server 2016
- Windows Server 2012 R2
- Windows Server 2012
- Windows Server 2008 R2
- Windows Server 2008
- Windows 10
- Windows 8.1
Command shell overview
The Command shell was the first shell built into Windows to automate routine tasks, like user account management or nightly backups, with batch (.bat) files. With Windows Script Host you could run more sophisticated scripts in the Command shell. For more information, see cscript or wscript. You can perform operations more efficiently by using scripts than you can by using the user interface. Scripts accept all Commands that are available at the command line.
Windows has two command shells: The Command shell and PowerShell. Each shell is a software program that provides direct communication between you and the operating system or application, providing an environment to automate IT operations.
PowerShell was designed to extend the capabilities of the Command shell to run PowerShell commands called cmdlets. Cmdlets are similar to Windows Commands but provide a more extensible scripting language. You can run Windows Commands and PowerShell cmdlets in Powershell, but the Command shell can only run Windows Commands and not PowerShell cmdlets.
For the most robust, up-to-date Windows automation, we recommend using PowerShell instead of Windows Commands or Windows Script Host for Windows automation.
You can also download and install PowerShell Core, the open source version of PowerShell.
Incorrectly editing the registry may severely damage your system. Before making the following changes to the registry, you should back up any valued data on the computer.
To enable or disable file and directory name completion in the Command shell on a computer or user logon session, run regedit.exe and set the following reg_DWOrd value:
To set the reg_DWOrd value, use the hexadecimal value of a control character for a particular function (for example, 0 9 is Tab and 0 08 is Backspace). User-specified settings take precedence over computer settings, and command-line options take precedence over registry settings.
Command-line reference A-Z
To find information about a specific command, in the following A-Z menu, click the letter that the command starts with, and then click the command name.
Split long commands in multiple lines through Windows batch file
How can I split long commands over multiple lines in a batch file?
6 Answers 6
You can break up long lines with the caret ^ as long as you remember that the caret and the newline following it are completely removed. So, if there should be a space where you’re breaking the line, include a space. (More on that below.)
would be written as:
The rule for the caret is:
A caret at the line end, appends the next line, the first character of the appended line will be escaped.
You can use the caret multiple times, but the complete line must not exceed the maximum line length of
8192 characters (Windows XP, Windows Vista, and Windows 7).
To suppress the escaping of the next character you can use a redirection.
The redirection has to be just before the caret. But there exist one curiosity with redirection before the caret.
If you place a token at the caret the token is removed.
And it is also possible to embed line feeds into the string:
The empty line is important for the success. This works only with delayed expansion, else the rest of the line is ignored after the line feed.
It works, because the caret at the line end ignores the next line feed and escapes the next character, even if the next character is also a line feed (carriage returns are always ignored in this phase).
(This is basically a rewrite of Wayne’s answer but with the confusion around the caret cleared up. So I’ve posted it as a CW. I’m not shy about editing answers, but completely rewriting them seems inappropriate.)
You can break up long lines with the caret ( ^ ), just remember that the caret and the newline that follows it are removed entirely from the command, so if you put it where a space would be required (such as between parameters), be sure to include the space as well (either before the ^ , or at the beginning of the next line — that latter choice may help make it clearer it’s a continuation).
Examples: (all tested on Windows XP and Windows 7)
can be written as:
(That last works because there are no spaces betwen the xc and the ^ , and no spaces at the beginning of the next line. So when you remove the ^ and the newline, you get. xcopy .)
For readability and sanity, it’s probably best breaking only between parameters (be sure to include the space).
Be sure that the ^ is not the last thing in a batch file, as there appears to be a major issue with that.