FreeKB - Bash (Scripting) append new lines to a file or variable
Bash (Scripting) - append new lines to a file or variable

The echo command is the easiest way to append a new line to the end of a file. In this example, "Hello World" will be appended to the end of example.txt.

echo "Hello World" >> example.txt


The sed command with the a (append) flag can be used to append lines to a file or variable. For example, let's say you have a file or variable with the following text.

Hello World


The following command will add "New Text" to the file or variable. The a flag in this command is used to append.

sed -i '/Hello World/a New Text' $foo


In this example, the file or variable will have the following output.

Hello World
New Text


Add a Comment

We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.

Please enter 5eb83 in the box below so that we can be sure you are a human.


Web design by yours truely - me, myself, and I   |   |