Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Echo newline to powershell console

Is it possible to write a newline to the console in powershell?

I tried echo "\n" but it is not translated to a new line, it just outputs \n.

like image 925
Black Avatar asked Apr 20 '16 07:04

Black


People also ask

How do I echo a newline in PowerShell?

Windows: `Echo` Newline (Line Break) [\n] – CMD & PowerShell. In a Windows Command Prompt (CMD) and PowerShell when you execute the echo command to print some text or redirect it to a file, you can break it into multiple lines. In Linux you can do this by using the \n .

How do you echo a newline character?

There are a couple of different ways we can print a newline character. The most common way is to use the echo command. However, the printf command also works fine. Using the backslash character for newline “\n” is the conventional way.

What is the newline character in PowerShell?

`n is used in Powershell to append a new line to the standard output. The use of `n in Powershell is similar to \n in C++ or Java. The backtick character (`) represents an escape character in Powershell. An escape character lets us print otherwise unprintable characters to the standard output.

How do I replace a new line in PowerShell?

You can use "\\r\\n" also for the new line in powershell .


2 Answers

echo is used in PowerShell all the time. It is an alias for write-output.

The issue here is that you need to be using the PowerShell escape character which is a backtick. You can read more about this on TechNet on about_escape_characters.

The following special characters are recognized by Windows PowerShell:

`0    Null `a    Alert `b    Backspace `f    Form feed `n    New line `r    Carriage return `t    Horizontal tab `v    Vertical tab 

So, if you are just trying to break up the output, you can simply use:

echo "`n" 

That will actually output two new lines as all strings sent to Write-Output (see Get-Alias echo) will be terminated with a new line regardless. Since strings are evaluated as expressions in PowerShell "" would also work but it would only output the one line.

Also, since this data is being sent to the standard output stream, it will be captured by variables and pipelines. Write-Host might be a better option if that is something you want to mitigate.

like image 110
Matt Avatar answered Sep 29 '22 09:09

Matt


Just do "" It will print a blank line

like image 20
Andrey Marchuk Avatar answered Sep 29 '22 09:09

Andrey Marchuk