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

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

no newline in echo/printf in BASH while loop

bashwhile-loopprintfechonewline

提问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 printfstatement:

您只需要\nprintf语句中添加换行符 , :

printf "(entity:content %s:%s)\n" $ENTITY $CONTENT

回答by Josh Jolly

printfdoesn'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