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 |
| Redirection + errors | command >> file.txt 2>&1 | Appending output and error messages |
| 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.txtNothing appears on the terminal, because the output went to the file instead. Assuming file.txt already contained a single line, display it to see the result:
cat file.txtan existing line
this is a new lineThe original content is untouched and the new text sits below it. That is the whole difference between >> and >.
When 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.txtEach \n starts a new line, so this single command appends two lines rather than one.
To produce more complex or formatted output, use the printf
command:
printf "Hello, I am %s.\n" "$USER" >> file.txtThe %s placeholder is replaced with the value of $USER, and the trailing \n ends the line. Unlike echo, printf never adds a newline on its own, so omitting \n would glue the text to whatever is already on the last line of the file.
You 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.txtRedirection with >> captures standard output only. When a command can also fail, add 2>&1 after the redirection to append its error messages to the same file:
find /etc -name "*.conf" >> results.txt 2>&1Bash 4 and later also accept the shorter &>> form, which appends both streams with a single operator:
find /etc -name "*.conf" &>> results.txtThe order matters in the first form. 2>&1 has to come after >> results.txt. If it comes first, standard error remains connected to wherever standard output pointed before the file redirection, usually the terminal. Our guide to redirecting stderr and stdout
explains why.
To 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)
EOFBoth $PWD and $(whoami) are expanded before the text is written, so the file receives the resolved values rather than the literal variable names. Display the two appended lines with tail:
tail -n 2 file.txtThe current working directory is: /home/linuxize
You are logged in as: linuxizeThe output shows the expanded working directory and username stored in the file.
When 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.txtthis is a new lineUnlike >>, tee echoes everything it writes, so the text appears on the terminal and at the end of the file at the same time. That makes it a good fit for scripts where you want a log entry saved and shown at once.
If 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 One File to Another Using cat
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.
If target.txt held a single line and source.txt held two, the merged file looks like this:
cat target.txtexisting line
line one
line twoPrepend Text to a File
Appending places text at the end of a file. The opposite operation, prepending, places it at the top. Redirection cannot do this on its own, because >> always writes to the end.
For a non-empty file, GNU sed
can insert text before line 1. Preview the result without changing the file:
sed '1i this is the first line' file.txtIf the preview is correct, use -i.bak to edit the file and retain the original as file.txt.bak:
sed -i.bak '1i this is the first line' file.txtDisplay the edited file to confirm that the new line is at the top:
cat file.txtthis is the first line
an existing line
this is a new lineThe backup keeps the previous file content and permissions available if you need to restore it.
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.
Errors still appear on the terminal after redirecting with >>
The >> operator redirects standard output only. Add 2>&1 after the redirection, as in command >> file.txt 2>&1, to send error messages to the same file.
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.
How do I append text without adding a newline?
Use printf without a trailing \n, as in printf 'text' >> file.txt. The echo command always ends its output with a newline unless you pass the -n option.
How do I add a line to the beginning of a file instead of the end?
On Linux, preview the change with sed '1i your text' file.txt, then apply it with sed -i.bak '1i your text' file.txt. The >> operator can only write to the end of a file, so prepending requires rewriting the file.
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