How can I insert a new line in a cmd.exe command?

22

8

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?

rudimenter

Posted 2010-06-08T09:38:12.217

Reputation: 321

Possible duplicate of How do I do single command line command on multiple lines in windows?

– phuclv – 2018-10-20T15:21:38.777

Answers

19

Use the ^ character as an escape:

command -option:text^

whatever

I'm assuming you're using cmd.exe from Windows XP or similar. This is not actual DOS. If you are using actual DOS (MS-DOS, Win3.1, Win95, Win98, WinME), then I do not believe there is an escape 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.

Darth Android

Posted 2010-06-08T09:38:12.217

Reputation: 35 133

Too cool, I did not know that. – T.J. Crowder – 2010-06-08T10:05:32.917

1This 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 – 2010-06-08T10:09:59.083

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 – 2010-06-08T19:17:57.043

@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 – 2010-06-08T19:23:48.197

1It 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 – 2011-01-28T05:40:05.903

1I 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 – 2011-01-28T05:41:43.873

Unfortunately there seems to be no way to include a newline character inside a quoted string. – Ian Goldby – 2012-08-03T10:51:31.353

4

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

Posted 2010-06-08T09:38:12.217

Reputation: 435

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 – 2018-05-24T17:53:47.010

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 – 2018-05-24T18:28:27.597

Weird! I wonder if I can dig up an old cmd.exe from something.... – Brad – 2018-05-24T18:33:23.113

@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 – 2018-05-24T18:53:13.483

Oh wow! Weird. Someone just commented that in powershell, I can use backtick-r-backtick-n, which works. – Brad – 2018-05-24T18:55:29.643

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 – 2018-10-18T16:57:01.977

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 – 2018-10-18T18:04:00.763

@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 – 2018-10-18T22:09:48.857

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!

Brent Rittenhouse

Posted 2010-06-08T09:38:12.217

Reputation: 113

1

^ '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 . . .

ilias

Posted 2010-06-08T09:38:12.217

Reputation: 21

1

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

Posted 2010-06-08T09:38:12.217

Reputation: 111

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.

user1686

Posted 2010-06-08T09:38:12.217

Reputation: 283 655