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

By 

Updated on

5 min read

Appending text to a file in Bash using the >> redirection operator

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 .

MethodCommandUse When
Redirectionecho "text" >> file.txtAppending to a single file
Heredoccat << EOF >> file.txtAppending multiple lines
teeecho "text" | tee -a file.txtAppending and displaying output
tee + sudoecho "text" | sudo tee -a file.txtAppending to a protected file
tee multipleecho "text" | tee -a f1.txt f2.txtAppending to multiple files at once
Append a filecat source.txt >> target.txtAdding 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:

Terminal
echo "this is a new line" >> file.txt

When used with the -e option, the echo command interprets backslash-escaped characters such as \n for newline:

Terminal
echo -e "this is a new line \nthis is another new line" >> file.txt

To produce more complex or formatted output, use the printf command:

Terminal
printf "Hello, I am %s.\n" "$USER" >> file.txt

You can append the output of any command to a file. Here is an example using the date command:

Terminal
date +"Year: %Y, Month: %m, Day: %d" >> file.txt

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:

sh
cat << EOF >> file.txt
The current working directory is: $PWD
You are logged in as: $(whoami)
EOF

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:

Terminal
echo "this is a new line" | tee -a file.txt

If you do not want tee to write to standard output, redirect it to /dev/null:

Terminal
echo "this is a new line" | tee -a file.txt >/dev/null

One 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:

Terminal
echo "this is a new line" | sudo tee -a file.txt

To append to more than one file at a time, pass multiple filenames as arguments:

Terminal
echo "this is a new line" | tee -a file1.txt file2.txt file3.txt

Append 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:

Terminal
cat source.txt >> target.txt

This 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:

Terminal
cat file1.txt file2.txt >> combined.txt

cat 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

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