38

I'm trying to execute a command on cmd.exe, with a line break or new line as part of the command, like below:

command -option:text  
whatever

But every new line executes the command, instead of continuing the command on the next line.

So how can I input a new line character, or create a multi-line command in cmd.exe?

HopelessN00b
  • 1,882
  • 3
  • 21
  • 29
rudimenter
  • 481
  • 1
  • 4
  • 4
  • 1
    Possible duplicate of [How do I do single command line command on multiple lines in windows?](https://superuser.com/questions/187410/how-do-i-do-single-command-line-command-on-multiple-lines-in-windows) – phuclv Oct 20 '18 at 15:21
  • 1
    [Long commands split over multiple lines in Windows Vista batch (.bat) file](https://stackoverflow.com/q/69068/995714) – phuclv Oct 20 '18 at 15:22

7 Answers7

39

Use the ^ character as an escape:

command -option:text^ 
whatever

I'm assuming you're using cmd.exe from Windows XP or later. This is not actual DOS. If you are using actual DOS (MS-DOS, Win3.1, Win95, Win98, WinME), then I don't believe there is an escape character for newlines. You would need to run a custom shell. cmd.exe will prompt you for More? each time you press enter with a ^ at the end of the line, just press enter again to actually escape/embed a newline.

Wasif
  • 7,984
  • 2
  • 19
  • 32
Darth Android
  • 37,872
  • 5
  • 94
  • 112
  • Too cool, I did not know that. – T.J. Crowder Jun 08 '10 at 10:05
  • 1
    This what you suggests is the allows me to enter more text on a newline. It does not preserve the newline. What i want is that between "text" and "whatever" is an newline. Something like this: "text\nwhatever" – rudimenter Jun 08 '10 at 10:09
  • I believe you have to use two newlines to do that, my bad for not realizing this in my original post (now edited) – Darth Android Jun 08 '10 at 19:17
  • @T.J. Crowder I know, I didn't either until I looked it up for another question on here a few days ago. – Darth Android Jun 08 '10 at 19:23
  • 1
    It should be noted that if you have spaces in the text, you will need to wrap them in quotes. Further, make sure that you include the end quote, otherwise the caret will be ignored as an escape character and treated like text. Finally, you will need to put quotes around the text on each line containing spaces (do so even if it doesn't contain spaces to be safe), and put a caret after the closing quote for each line that continues. – Synetech Jan 28 '11 at 05:40
  • 1
    I ended up here by trying to find a way to put newlines in the comments of a `SHUTDOWN.EXE` command. I ended up using something like this: `shutdown -s -t 900 -c "foo bar"^` **> More?** `"baz"^` **> More?** `"test that."` It worked. – Synetech Jan 28 '11 at 05:41
  • 1
    Unfortunately there seems to be no way to include a newline character inside a quoted string. – Ian Goldby Aug 03 '12 at 10:51
  • Yeah, DOS COMMAND.COM was soooooo horrible !! – Wasif Sep 21 '20 at 06:21
6

Use Alt codes with the numpad

C:\>ver

Microsoft Windows [Version 6.3.9600]

C:\>echo Line1◙Line2 >con
Line1
Line2

C:\>

That line feed character can be entered as an Alt code on the CLI using the numpad: with NumLock on, hold down ALT and type 10 on the numpad before releasing ALT. If you need the CR as well, type them both with Alt+13 and then Alt+10 : ♪◙

Note: this will not work in a batch file.

Sample use case:

You are trying to read the %PATH% or %CLASSPATH% environment variables to get a quick overview of what's there and in what order - but the wall of text that path returns is unreadable. Then this is something you can quickly type in:

echo %path:;=◙% >con

Edit:

Added the >con workaround that Brent Rittenhouse discovered for newer versions of cmd.exe, where the original method had stopped working.

Amit Naidu
  • 549
  • 7
  • 10
  • This doesn't seem to work for me. I end up with `e2 99 aa e2 97 99` passed in. Some sort of unicode code point? I do see the characters go in on the command line though. Any suggestions? – Brad May 24 '18 at 17:53
  • 1
    @Brad I can confirm it doesn't seem to be working any more in the new cmd.exe in Windows 10 and even my old Windows 7 box won't behave this way anymore. – Amit Naidu May 24 '18 at 18:28
  • Weird! I wonder if I can dig up an old cmd.exe from something.... – Brad May 24 '18 at 18:33
  • @Brad, I just tried and few things and realized that you can get that old behavior if you switch to Raster Fonts in console properties, instead of Lucida and True Type fonts and check the `Use legacy console` checkbox. But it looks really bad on a high res monitor. – Amit Naidu May 24 '18 at 18:53
  • Oh wow! Weird. Someone just commented that in powershell, I can use backtick-r-backtick-n, which works. – Brad May 24 '18 at 18:55
  • The craziest thing about this is that if you have a) use legacy console and b) raster fonts like specified it will work, and then if you switch the font to Lucida Console (or whatever it's called) it will switch and you'll see the text still with the new line in it, but if you try it now it won't work. Even if you copy the line you JUST did and re-paste it (I thought maybe it was a differnet code page or something) it STILL won't work. Example: https://i.imgur.com/kKX2jiy.png – Brent Rittenhouse Oct 18 '18 at 16:57
  • 1
    Actually, I think I've found a solution that works for this completely in ANY font! If you simply echo out the line with alt-10 characters and then redirect it to CON it will work! For example: `(echo New-line detected rigghhhtt ◙^<--- HERE!) > CON` The surrounding parenthesis are optional. Here is a more robust example of what you can do with this (AND carriage returns which now work, and completely independently!): https://i.imgur.com/qqN37PH.png – Brent Rittenhouse Oct 18 '18 at 18:04
  • @BrentRittenhouse Wow, nicely done! I tested it with my `path` echo and it works beautifully now. I have missed this for a long time, so thank you ! `echo %path:;=◙% >con`. @Brad, heads up. – Amit Naidu Oct 18 '18 at 22:09
2

^'s output are saveable.

set br= ^
<</br (newline)>>
<</br>>

Example:

@echo off
setlocal enableExtensions enableDelayedExpansion
rem cd /D "%~dp0"


rem //# need 2 [/br], can't be saved to a var. by using %..%;
set br= ^



set "t=t1!br!t2!br!t3"

for /f "tokens=* delims=" %%q in ("!t!") do (
    echo %%q
)


:scIn
rem endlocal
pause
rem exit /b

; output:

t1
t2
t3
Press any key to continue . . .
Wasif
  • 7,984
  • 2
  • 19
  • 32
ilias
  • 131
  • 4
2

I don't know if this will work for you but by putting &echo (the following space is important). in between each statement that you want on a new line. I only tried this with a simple bat file of

echo %1

Then saved that as testNewLines.bat

So then the cmd line

testNewLines first line&echo Second Line&echo Third line

Resulted in the following being echoed back

first line
second line
Third line

Haydar
  • 121
  • 2
1

Expanding on @Amit's answer (https://superuser.com/a/1132659/955256) I found a way to do it in any font WITHOUT using the legacy console.

All you need to do is simply echo out the line with ALT-10 characters and then redirect it to con and it will work!

For example, given:

(echo New-line detected rigghhhtt ◙^<--- HERE!) > CON

edit: Fun fact, it seems that you can put the redirection at the beginning like so:

> CON echo New-line detected rigghhhtt ◙^<--- HERE!

Weird, eh?

You will get an output of:


New-line detected rigghhhtt  
<-- HERE!

(The surrounding parenthesis are optional.)

Here is a more robust example of what you can do with this AND carriage returns which now work, and completely independently!:

Best of all, this works if you redirect to a file as well!
(simply change > CON to > desired_output_file.txt

Enjoy!

Wasif
  • 7,984
  • 2
  • 19
  • 32
0

I believe you can't do that from the Windows cmd.exe shell. (It is not DOS.)


You do not need a full "custom shell" for that. You will only need to write something like this (example in Python):

import subprocess
subprocess.call(["C:\\bin\\sometool.exe", "test\nwith\nnewlines"])

Or Ruby:

Kernel::exec "C:\\bin\\sometool.exe", "test\nwith\nnewlines"

See, it's not that hard.

u1686_grawity
  • 426,297
  • 64
  • 894
  • 966
0

Install Powershell Core

Inside your batch file:

MyBatchFile.bat

@echo off
echo Hello
pwsh.exe -Command [System.Console]::WriteLine()
echo world
Wasif
  • 7,984
  • 2
  • 19
  • 32
Joma
  • 131
  • 3