Sometimes you may be required to write or append multiple lines to a file. You can use multiple methods to write multiple lines to a file through the command line in the Linux system. Here are the three methods described below.
You can write/append content line by line using the multiple echo commands. This the simple and straight forward solution to do this. We recommend going with Method 2 or Method 3.
echo " line 1 content" >> myfile.txt echo " line 2 content" >> myfile.txt echo " line 3 content" >> myfile.txt
You can append content with the multi-line command in the quoted text. In this way can write multiple lines to fine with single echo command. But the third method is our suggested method to do this.
echo " line 1 content line 2 content line 3 content" >> myfile.txt
This is the third and suggested option to use here documents (<<) to write a block of multiple lines text in a single command. The above methods are also useful but personally, I also use this while working.
cat >> greetings.txt <<EOL line 1 content line 2 content line 3 contentEOL