How to Write/Append Multiple Lines to a File on Linux

This article describes how to add lines to a file in linux.

You can write/append content line by line using the multiple echo commands.

echo "line 1 content" >>newfile.txt

You can append content with the multi-line command in the quoted text.

echo "line 1 content
line 2 content
line 3 content" >> newfile.txt

Write a block of multiple lines text in a single command.

cat >> newfile.txt <<EOL
line 1 content
line 2 content
line 3 content
EOL

Using a bash file.

Use the bash file to add multiple lines.

nano addlines.sh

Add the lines:

#!/bin/bash
cat >> newfile.txt <<EOL
line 1 content
line 2 content
line 3 content
EOL

Save the file and then change the permissions to chmod +x addlines.sh

chmod +x addlines.sh

Execute the file

./addlines.sh

See the results:

cat newfile.txt
line 1 content
line 2 content
line 3 content

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.