Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

no newline in echo/printf in BASH while loop

why on executing following script each printf (tried also with echo) is printed on the same line??

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:)
like image 340
Kamil Roman Avatar asked Mar 13 '14 17:03

Kamil Roman


People also ask

How to use the ECHO command to print newlines in Linux?

Using the backslash character for newline “ ” is the conventional way. However, it’s also possible to denote newlines using the “$” sign. The echo command takes a string as input and prints it out on the console screen. To print any text, we use the echo command in the following manner: As mentioned earlier, the newline character is “ ”, right?

How do I make a bash script not print a newline?

On the default bash implementation, that means (from help echo ): Make it into something that isn't an option by including another character. For example, tell echo not to print a newline with -n, then tell it to interpret backslash escapes with -e and add the newline explicitly.

How to prevent echo from printing backslash escapes?

For example, tell echo not to print a newline with -n, then tell it to interpret backslash escapes with -e and add the newline explicitly. That, however, adds a space which you probably don't want. Use a non-printing character before it. Here.

How do you print a new line in Linux terminal?

The most common way is to use the echo command. However, the printf command also works fine. Using the backslash character for newline “ ” is the conventional way. However, it’s also possible to denote newlines using the “$” sign. The echo command takes a string as input and prints it out on the console screen.


1 Answers

You'll just need to add the newline character, \n, to the printf statement:

printf "(entity:content %s:%s)\n" $ENTITY $CONTENT
like image 67
newfurniturey Avatar answered Sep 23 '22 10:09

newfurniturey