Shell – how to write multiple lines to a file using shell script

shellxml

I need to read data from a source and have to form a XML file using shell script. But first of all i don't know how to write multiple lines in a file using shell script

Best Answer

Use output redirection

echo '<fruit>'    > foo.xml   # overwrites
echo ' <apple />' >> foo.xml  # appends
echo '</fruit>    >> foo.xml  # appends

Or use a "here document"

cat <<EndXML > foo.xml
<fruit>
 <apple />
</fruit>
EndXML

Better yet is to use a scripting/programming language that has support for XML. I like Perl and XML::LibXML but you may prefer something else.

Related Question