311

How do we extend a command to the next line?

Basically what's the Windows alternative for Linux's:

ls -l \
/usr/

Here we use backslashes to extend the command onto the next lines.

What's the equivalent for Windows?

3

7 Answers 7

486

After trying almost every key on my keyboard:

C:\Users\Tim>cd ^
Mehr? Desktop

C:\Users\Tim\Desktop>

So it seems to be the ^ key.

Note that ^ trailing works in batch files as well (tested on Windows 7).

Sign up to request clarification or add additional context in comments.

4 Comments

Microsoft can't comply with its own standards so this does not work in PowerShell. The PowerShell Equivalent to ^ is ` So C:\Users\Tim>cd ` >> Desktop Gets you to Tim's Desktop \0/
@RicJafe Why should it? PowerShell is completely separate! The equivalent key for PS is `.
Any idea on what it is in powershell?
@RyanSeanWattrus Using the same very scientific method: ` (l'accent grave)
88

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):

C:\> dir ^
More? C:\Windows

1 Comment

'^' character should be last character in line. Otherwise if you have SPACE at end, multiline command does not work.
68

Solution 1 In CMD, Use ^ , example

docker run -dp 3000:3000 ^
  -w /app -v "$(pwd):/app" ^
  --network todo-app ^
  -e MYSQL_HOST=mysql ^
  -e MYSQL_USER=root ^
  -e MYSQL_PASSWORD=secret ^
  -e MYSQL_DB=todos ^
  node:12-alpine ^
  cmd "npm install && yarn run start"

Solution 2 In PowerShell, Use ` (backtick) , Example

docker run -dp 3000:3000 `
  -w /app -v "$(pwd):/app" `
  --network todo-app `
  -e MYSQL_HOST=mysql `
  -e MYSQL_USER=root `
  -e MYSQL_PASSWORD=secret `
  -e MYSQL_DB=todos `
  node:12-alpine `
  cmd "npm install && npm run start"

Comments

15

The caret character works, however the next line should not start with double quotes. e.g. this will not work:

C:\ ^
"SampleText" ..

Start next line without double quotes (not a valid example, just to illustrate)

1 Comment

This is true. Just add a space before the double quotes to make it work.
8

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 elses. But if you just want a multi-line conditional command to execute directly from CMD and not via a batch file, this should work well.

Let's say you have something like this in a batch that you want to run directly in command prompt:

@echo off
for /r %%T IN (*.*) DO (
    if /i "%%~xT"==".sln" (
        echo "%%~T" is a normal SLN file, and not a .SLN.METAPROJ or .SLN.PROJ file
        echo Dumping SLN file contents
        type "%%~T"
    )
)

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:

@echo off ^
More? for /r %T IN (*.sln) DO (^^
More? if /i "%~xT"==".sln" (^^
More? echo "%~T" is a normal SLN file, and not a .SLN.METAPROJ or .SLN.PROJ file^^
More? echo Dumping SLN file contents^^
More? type "%~T"))

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:

@echo off
for /r %T IN (*.sln) DO if /i "%~xT"==".sln" echo "%~T" is a normal SLN file, and not a .SLN.METAPROJ or .SLN.PROJ file | echo Dumping SLN file contents | type "%~T"

Comments

2

In windows 10 22H2, simply press enter after an open parenthesis gives you same result as typing ^ at the end of line. For example, after typing this line of code:

>for %f in (*) do (

press enter and a new line like this will pop up automatically:

More? 

1 Comment

I'm reasonably certain this has been true for many years.
1

You can even split string quoted parameters. End the line with "^ and start the next line with ". E.g.

dir "\users"

can be split as:

dir "\use"^
"rs"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.