27

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?

2
28

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.

8
  • Too cool, I did not know that. – T.J. Crowder Jun 8 '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 8 '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 8 '10 at 19:17
  • 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 5: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 5:41
5

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.

8
  • 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
  • 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!): i.imgur.com/qqN37PH.png – Brent Rittenhouse Oct 18 '18 at 18:04
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

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

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.

0

Install Powershell Core

Inside your batch file:

MyBatchFile.bat

@echo off
echo Hello
pwsh.exe -Command [System.Console]::WriteLine()
echo world
1
  • Powershell comes by default on Windows 7+ – programmer365 Sep 21 '20 at 6:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.