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
whateverBut 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?
27 Answers
Use the ^ character as an escape:
command -option:text^
whateverI'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.
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:;=◙% >conEdit:
Added the >con workaround that Brent Rittenhouse discovered for newer versions of cmd.exe, where the original method had stopped working.
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 %1Then saved that as testNewLines.bat
So then the cmd line
testNewLines first line&echo Second Line&echo Third lineResulted in the following being echoed back
first line
second line
Third line
^'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 . . . Expanding on @Amit's answer () 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!
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.
Install Powershell Core
Inside your batch file:
MyBatchFile.bat
@echo off
echo Hello
pwsh.exe -Command [System.Console]::WriteLine()
echo world 1