bash 如何使用 printf 添加换行符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10693431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 22:08:06  来源:igfitidea点击:

How do I add a newline using printf?

bashprintingterminal

提问by Nightlock32

How do I add a new line to a print command using printf?

如何使用 printf 在打印命令中添加新行?

printf "I want this on a new line!"

I thought it would be something like this but it didn't work

我以为它会是这样的,但它没有用

printf "/n I want this on a new line!/n"

Thanks in advance for the help!

在此先感谢您的帮助!

回答by petschekr

To write a newline use \nnot /nthe latter is just a slash and a n

要编写换行符,请\n不要/n使用后者只是一个斜杠和一个

回答by Paused until further notice.

Try this:

尝试这个:

printf '\n%s\n' 'I want this on a new line!'

That allows you to separate the formatting from the actual text. You can use multiple placeholders and multiple arguments.

这允许您将格式与实际文本分开。您可以使用多个占位符和多个参数。

quantity=38; price=142.15; description='advanced widget'
$ printf '%8d%10.2f  %s\n' "$quantity" "$price" "$description"
      38    142.15  advanced widget