BASH while 循环中的 echo/printf 中没有换行符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22386203/
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
no newline in echo/printf in BASH while loop
提问by Kamil Roman
why on executing following script each printf (tried also with echo) is printed on the same line??
为什么在执行以下脚本时,每个 printf(也尝试使用 echo)打印在同一行上?
function read_dom () {
local IFS=\>
read -d \< ENTITY CONTENT
}
cat my_xml_file.xml | \
{ while read_dom; do
printf "(entity:content %s:%s)" $ENTITY $CONTENT
}
Now, this produces a single line output:
现在,这会产生单行输出:
(entity:content member:)(entity:content name:id)(entity:content /name:)
How do I change this to multiline, like:
如何将其更改为多行,例如:
(entity:content member:)
(entity:content name:id)
(entity:content /name:)
回答by newfurniturey
You'll just need to add the newline character, \n
, to the printf
statement:
您只需要\n
在printf
语句中添加换行符 , :
printf "(entity:content %s:%s)\n" $ENTITY $CONTENT
回答by Josh Jolly
printf
doesn't append a newline as standard behaviour, you need to add it to your print string:
printf
不附加换行符作为标准行为,您需要将其添加到打印字符串中:
printf "(entity:content %s:%s)\n" $ENTITY $CONTENT