Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

windows - Echo newline to powershell console

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...