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

by
Jeremy Canfield |
Updated: May 12 2020
| Bash (Scripting) articles
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
Did you find this article helpful?
If so, consider buying me a coffee over at