Bash Append to File: >>, tee, and Heredoc Examples

In Bash, appending text to a file means adding content to the end of the file without overwriting what is already there. The >> redirection operator is the most direct way to do this, but the tee command and heredoc syntax offer additional flexibility, especially when appending to multiple files or to files that require elevated permissions.
To append to a file, you need write permissions on it. Otherwise, you will receive a Permission denied error.
This guide explains how to append to files in Bash with practical examples for each method.
Quick Reference
For a printable quick reference, see the Bash cheatsheet .
| Method | Command | Use When |
|---|---|---|
| Redirection | echo "text" >> file.txt | Appending to a single file |
| Heredoc | cat << EOF >> file.txt | Appending multiple lines |
| tee | echo "text" | tee -a file.txt | Appending and displaying output |
| tee + sudo | echo "text" | sudo tee -a file.txt | Appending to a protected file |
| tee multiple | echo "text" | tee -a f1.txt f2.txt | Appending to multiple files at once |
| Append a file | cat source.txt >> target.txt | Adding one file’s contents to another |
Append to a File Using Redirection
Redirection allows you to capture the output from a command and send it to a file. The >> operator appends the output to the end of a file without overwriting existing content.
The two most commonly used commands for printing text to standard output are echo and printf. To append their output to a file, specify the filename after the >> operator:
echo "this is a new line" >> file.txtWhen used with the -e option, the echo
command interprets backslash-escaped characters such as \n for newline:
echo -e "this is a new line \nthis is another new line" >> file.txtTo produce more complex or formatted output, use the printf
command:
printf "Hello, I am %s.\n" "$USER" >> file.txtYou can append the output of any command to a file. Here is an example using the date
command:
date +"Year: %Y, Month: %m, Day: %d" >> file.txtTo append multiple lines at once, use a Here document (heredoc). It is a type of redirection that passes a block of text as input to a command. The following example passes content to the cat
command and appends it to a file:
cat << EOF >> file.txt
The current working directory is: $PWD
You are logged in as: $(whoami)
EOFWhen appending to a file using redirection, always use >>. Using the single > operator will overwrite the entire file instead of appending to it.
Append to a File Using tee
The tee
command reads from standard input and writes to both standard output and one or more files simultaneously.
By default, tee overwrites the specified file. To append instead, use the -a (--append) option:
echo "this is a new line" | tee -a file.txtIf you do not want tee to write to standard output, redirect it to /dev/null:
echo "this is a new line" | tee -a file.txt >/dev/nullOne advantage of tee over >> is that it works with sudo, allowing you to append to files owned by root or another user. When combined with sudo, tee runs with elevated privileges and can write to protected files:
echo "this is a new line" | sudo tee -a file.txtTo append to more than one file at a time, pass multiple filenames as arguments:
echo "this is a new line" | tee -a file1.txt file2.txt file3.txtAppend the Contents of One File to Another
A common task is adding the contents of one file to the end of another. Redirect the output of cat with the >> operator:
cat source.txt >> target.txtThis reads source.txt and appends every line to target.txt, leaving the original target content in place. To merge several files into one, list them all before the operator:
cat file1.txt file2.txt >> combined.txtcat concatenates the files in the order given and appends the result to combined.txt.
Troubleshooting
File content is overwritten instead of appended
You used > instead of >>. The single > operator truncates the file before writing. Always use >> to preserve existing content.
Permission denied when appending
Your user does not have write permission on the file. Use sudo tee -a instead of >>: echo "text" | sudo tee -a file.txt.
Heredoc content has unexpected indentation
Tabs inside the heredoc body are preserved literally in the output. Use <<-EOF instead of <<EOF to strip leading tabs from each line.
FAQ
What is the difference between > and >>?> overwrites the file from the beginning, destroying any existing content. >> appends to the end of the file and leaves existing content intact. Always double-check the operator before redirecting to an important file.
How do I append a blank line to a file?
Run echo >> file.txt. Without arguments, echo outputs an empty line, which >> appends to the file.
How do I append to a file inside a Bash script?
Use the same >> operator or tee -a inside the script. All methods in this guide work identically within scripts.
How do I append to a file I do not have permission to write to?
Pipe the output to tee -a with sudo: echo "text" | sudo tee -a /etc/file.conf. This runs tee as root while keeping your own shell session unprivileged.
Conclusion
The >> redirection operator is the simplest way to append text to a file in Bash. Use tee -a when you need to append to multiple files at once or when the target file requires elevated permissions. To send only error messages to a file, see how to redirect stderr
.
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page