How to enter a multi-line command
Is it possible to split a PowerShell command line over multiple lines?
In Visual Basic I can use the underscore ( _ ) to continue the command in the next line.
13 Answers 13
You can use a space followed by the grave accent (backtick):
However, this is only ever necessary in such cases as shown above. Usually you get automatic line continuation when a command cannot syntactically be complete at that point. This includes starting a new pipeline element:
will work without problems since after the | the command cannot be complete since it’s missing another pipeline element. Also opening curly braces or any other kind of parentheses will allow line continuation directly:
Similar to the | a comma will also work in some contexts:
Keep in mind, though, similar to JavaScript’s Automatic Semicolon Insertion, there are some things that are similarly broken because the line break occurs at a point where it is preceded by a valid statement:
Finally, strings (in all varieties) may also extend beyond a single line:
They include the line breaks within the string, then.
I just found out that there must not be any character between the back tick and the line break. Even whitespace will cause the command to not work.
In most C-like languages I am deliberate about placing my braces where I think they make the code easiest to read.
PowerShell’s parser recognizes when a statement clearly isn’t complete, and looks to the next line. For example, imagine a cmdlet that takes an optional script block parameter:
if the script block is very long, you might want to write:
But this won’t work: the parser will see two statements. The first is Get-Foo and the second is a script block. Instead, I write:
I could use the line-continuation character (`) but that makes for hard-to-read code, and invites bugs.
Because this case requires the open brace to be on the previous line, I follow that pattern everywhere:
Note that if statements require a script block in the language grammar, so the parser will look on the next line for the script block, but for consistency, I keep the open brace on the same line.
Simlarly, in the case of long pipelines, I break after the pipe character ( | ):
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:
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.
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:
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.