In Linux, we constantly work with files. As a result, we may encounter instances where we need to append multiple lines to a file.

This quick guide will discuss various approaches you can use to append multiple lines in a file.

Method # 1 – Using echo & Printf

The simplest way to append multiple lines to a file is to use the echo and printf command.

Let us start with echo.

Echo is a command used to output a string or multiple strings as arguments. Echo is available in all Linux distributions, making it a good tool for redirecting output to a file.

Let us start by verifying the file is not empty.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-241.png60f5adec02d95.jpg" data-lazy- height="87" src="data:image/svg xml,” width=”444″>

As shown in the output above, the file is not empty; it contains lines of text.

To add multiple lines to a file with echo, use the -e option and separate each line with n.

When you use the -e option, it tells echo to evaluate backslash characters such as n for new line.

echo -e “Hello, worldnBash scripting is awesomenThis is a new line” >> multiple.txt

If you cat the file, you will realize that each entry is added on a new line immediately after the existing content.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-242.png60f5adec7181e.jpg" data-lazy- height="140" src="data:image/svg xml,” width=”626″>

The printf

Let us now look at printf. Using the printf command is very similar to the echo command. To append multiple lines with printf command:

printf “Learn Bash scripting basics herenhttps://linuxhint.com/bash_scripting_tutorial_beginners/” >> multiple.txt

Similarly, cating the command provides a similar output as:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-245.png" data-lazy- height="173" src="data:image/svg xml,” width=”664″>

Method # 2 – Using Bash Heredoc

Another method we can use to append multiple lines to a file in bash is to use the heredoc.

A heredoc is a redirection feature that allows you to pass multiple lines to a command or a file.

Using a heredoc involves specifying a delimiter at the beginning of your command. Once the shell encounters the delimiter, it terminates the input and redirects it to a file or a specific command.

Learn the basics of using a heredoc.

We can use the tee or cat command to redirect multiple lines to a file using a heredoc. Let’s discuss both

The Tee command

To append a line using the tee command, we use the -a option. We can then pass the input from a heredoc as:

tee -a multiple.txt <<EOF


Copy standard input toeach FILE, and also to standard output.


       -a, –append


appendtothe given FILEs, donot overwrite


       -i, –ignore-interrupts


              ignore interrupt signals


       -p     diagnose errors writing tonon pipes


–output-error[=MODE]

set behavior onwriteerror.  SeeMODEbelow


–help display this help and exit


–version


              output version information and exit


EOF

Once you execute the command, a tee will display the contents and append them to the specified file.

You can verify the content is appended to the file using cat.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-246.png" data-lazy- height="358" src="data:image/svg xml,” width=”731″>

The cat command

Using the cat command to append to a file is similar to using the tee command. However, we use the append redirection symbols to redirect the output.

The following command appends the following lines to the file.

cat multiple.txt

————————————————————


CAT APPENDS FILES ARE LOCATED HERE

————————————————————


EOF

Verify the contents exists in the file using the cat command:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-248.png" data-lazy- height="411" src="data:image/svg xml,” width=”708″>

To Conclude

This guide discussed various methods you can use to append multiple lines to a file in bash.

About the author

<img alt="" data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/john-150×150.png60f5adeecb85a.jpg" height="112" src="data:image/svg xml,” width=”112″>

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list