How to write a multiline command?
Asked Answered
S

7

289

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?

Stadiometer answered 3/3, 2009 at 9:22 Comment(3)
+1 for short and concise Q&A, more extended exploration of caret handling at #69568Backdrop
Possible duplicate of Long commands split over multiple lines in Windows Vista batch (.bat) fileAcme
The actual problem here is merely team members or legacy systems forcing developers to work on one operating system (here windows) instead of their favorite one (linux)Rube
F
56

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"
Featherhead answered 15/7, 2022 at 1:41 Comment(0)
J
457

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

Judon answered 3/3, 2009 at 9:31 Comment(4)
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/Ligni
@RicJafe Why should it? PowerShell is completely separate! The equivalent key for PS is `.Complication
Any idea on what it is in powershell?Gamal
@RyanSeanWattrus Using the same very scientific method: ` (l'accent grave)Judon
B
83

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
Bolshevist answered 3/3, 2009 at 10:5 Comment(1)
'^' character should be last character in line. Otherwise if you have SPACE at end, multiline command does not work.Shrunken
F
56

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"
Featherhead answered 15/7, 2022 at 1:41 Comment(0)
C
14

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)

Crowbar answered 14/3, 2018 at 12:35 Comment(1)
This is true. Just add a space before the double quotes to make it work.Oriole
E
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"
Ergot answered 15/2, 2019 at 0:8 Comment(0)
G
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? 
Gentilesse answered 2/12, 2022 at 17:4 Comment(1)
I'm reasonably certain this has been true for many years.Bunch
H
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"
Hunnish answered 28/4, 2023 at 16:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.