If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.
Linux Commands for Appending Multiple Lines to a File
Last modified: November 8, 2020
1. Overview
In this quick tutorial, we’re going to take a look at how we can append multiline strings to a file using the tools provided by our command shell.
For simplicity we’ll use the same set of test data in all the examples:
Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
The commands used in this tutorial were tested in Bash but should work in other POSIX-compliant shells as well, unless specified otherwise.
2. echo
We can redirect the output of echo and append it to our file using the redirection operator >>. A new line is inserted automatically when the file doesn’t exist and is not empty.
Therefore a naive solution to our problem would be to call echo twice, once for each line that we need to append:
echo Lorem ipsum dolor sit amet, consectetur adipiscing elit, >> target.txt
echo sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. >> target.txt
Let’s further simplify this and use a oneliner by explicitly inserting the newline character \n. We use the -e argument to force evaluation, which is required when we execute the statement within a script:
echo -e Lorem ipsum dolor sit amet, consectetur adipiscing elit,\\nsed do eiusmod \
tempor incididunt ut labore et dolore magna aliqua. >> target.txt
In the above example, we have to explicitly escape the \ with another \. However, we can avoid this by surrounding our string with double quotes, which is regarded as best practice:
echo -e "Lorem ipsum dolor sit amet, consectetur adipiscing elit,\nsed do eiusmod \
tempor incididunt ut labore et dolore magna aliqua." >> target.txt
It’s important to note that the echo command has one serious drawback. Many shells provide their own version of echo. Consequently, implementations are not always POSIX-compliant and can differ between shells.
3. printf
Let’s now take a look at the printf command. This command works the same in every POSIX-compliant shell and can be used in the same way as echo:
printf "Lorem ipsum dolor sit amet, consectetur adipiscing elit,\nsed do eiusmod \
tempor incididunt ut labore et dolore magna aliqua." >> target.txt
Furthermore, the printf command is more advanced and can be used to format strings as well.
It’s similar to the C-function that goes by the same name. For example, we can use a C-style string format like this:
printf "%s\n%s" "Lorem ipsum dolor sit amet, consectetur adipiscing elit," \
"sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
More information about formatting strings can be found in the comprehensive man pages.
4. cat
There are solutions to our problem that are less obvious.
For example, we can use the cat command for appending lines to a file. Normally, we use this command to concatenate files and print their contents to the standard output.
However, if we omit the file argument, cat will read input from the standard input. Then we can use cat to append text:
cat << EOF >> target.txt
Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
EOF
In this example, << EOF means that we want cat to read from the standard input until it encounters a line only containing the text EOF (end of file). Likewise, the output of cat is written to target.txt in the same way as in previous examples, using the >> redirection operator.
5. tee
Simply put, the tee command takes standard input and copies it to standard output, also making copies of it in zero or more files. We can use tee in a similar way to how we used cat previously.
For example, we can tell tee to append (-a) standard input to our file until EOF is encountered:
tee -a target.txt << EOF
Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
EOF
6. Conclusion
In this quick tutorial, we looked at how we can use default shell commands to append multiline strings to a file.
We began by looking at the echo and printf commands which are the most obvious solutions. Then, we showed how we can achieve similar results using two less common approaches using the cat and tee commands.
As always, the full source code of the article is available over on GitHub.