循环中BASH中的echo / printf没有换行符

时间:2014-03-13 17:22:23

标签: bash while-loop printf echo newline

为什么在执行以下脚本时每个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
}

现在,这会产生一行输出:

(entity:content member:)(entity:content name:id)(entity:content /name:)

如何将其更改为多行,例如:

(entity:content member:)
(entity:content name:id)
(entity:content /name:)

2 个答案:

答案 0 :(得分:4)

您只需要将换行符\n添加到printf语句中:

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

答案 1 :(得分:4)

printf不会将换行符附加为标准行为,您需要将其添加到打印字符串中:

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