将换行符回显到 powershell 控制台 [英] Echo newline to powershell console

查看:28
本文介绍了将换行符回显到 powershell 控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 powershell 中向控制台写入换行符?

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

我试过 echo "\n" 但它没有转换成新行,它只是输出 \n.

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

推荐答案

echo 一直在 PowerShell 中使用.它是 write-output 的别名.

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

这里的问题是您需要使用 PowerShell 转义字符,它是一个反引号.您可以在 TechNet about_escape_characters 上阅读更多相关信息.

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.

Windows PowerShell 可识别以下特殊字符:

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"

这实际上会输出两个新行,因为所有发送到 Write-Output(参见 Get-Alias echo)的字符串都将以新行终止.由于字符串在 PowerShell 中被评估为表达式 "" 也可以工作,但它只会输出一行.

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.

此外,由于此数据正在发送到标准输出流,因此它将被变量和管道捕获.Write-Host 可能是一个更好的选择,如果你想缓解这种情况.

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.

这篇关于将换行符回显到 powershell 控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆