How to append multiple lines?

0108

To append multiple lines to a file in Linux, you can use the echo command with the >> operator, or you can use a here document. Here are two methods:

Method 1: Using echo with newline characters

You can use echo with the -e option to interpret escape sequences like \n for new lines:

echo -e "First line\nSecond line\nThird line" >> filename.txt

Method 2: Using a Here Document

A here document allows you to append multiple lines more conveniently:

  1. Open your terminal.
  2. Use the following syntax:
cat <<EOL >> filename.txt
First line
Second line
Third line
EOL

Replace filename.txt with the name of your file. The EOL can be any delimiter you choose; just make sure to use the same one at the beginning and end.

Example

To append multiple lines to a file named notes.txt, you can use:

cat <<EOL >> notes.txt
This is the first line.
This is the second line.
This is the third line.
EOL

This will add the three lines to notes.txt.

0 Comments

no data
Be the first to share your comment!