Bash: Write to File

When you write a Bash script, you often need to capture command output, save logs, or generate small configuration files on the fly. Bash gives you a few ways to do this directly from the shell, without reaching for a separate editor.
This guide explains how to write text to a file in Bash using the redirection operators (> and >>) and the tee command, including how to create new files, append to existing ones, and write to files that require root privileges.
Writing to a File Using Redirection Operators
In Bash, the redirection of output allows you to capture the output from a command and write it to a file.
The general format for redirecting and writing output to a file is as follows:
output > filename
output >> filename- The
>redirection operator writes the output to a given file. If the file exists, it is truncated to zero length. Otherwise, the file is created. Be extra careful when using this operator as you may overwrite an important file. - The
>>redirection operator appends the output to a given file. The file is created if it does not exist.
You need to have write permissions to the file. Otherwise, you will receive a permission denied error.
Here is a simple example showing how to redirect the output of the echo
command to a file:
echo "this is a line" > file.txtTo prevent overwriting existing files, enable the “noclobber” option with the set builtin:
set -o noclobber
echo "this is a line" > file.txtbash: file.txt: cannot overwrite existing fileThe >| operator allows you to override the Bash “noclobber” option:
set -o noclobber
echo "this is a line" >| file.txtThe >> operator appends the output to the end of the file, rather than overwriting it:
echo "this is a line" >> file.txtUse the printf
command to create more complex output:
printf "Hello, I am %s.\n" "$USER" > file.txtIf you want to write multiple lines to a file, use the Here document (Heredoc) redirection.
For example, you can pass the content to the cat
command and write it to a file:
cat << EOF > file.txt
The current working directory is: $PWD
You are logged in as $(whoami)
EOFTo append the lines, replace > with >> before the file name:
cat << EOF >> file.txt
The current working directory is: $PWD
You are logged in as $(whoami)
EOFYou can write the output of any command to a file:
date +"Year: %Y, Month: %m, Day: %d" > file.txtThe output of the date
command will be written to the file.
Writing to a File Using the tee Command
The tee
command reads from the standard input and writes to both standard output and one or more files simultaneously.
echo "this is a line" | tee file.txtThe default behavior of tee is to overwrite the specified file, same as the > operator. To append the output to the file, invoke the command with the -a (--append) option:
echo "this is a line" | tee -a file.txtIf you do not want tee to write to the standard output, you can redirect it to /dev/null:
echo "this is a line" | tee file.txt >/dev/nullTo write the text to more than one file, specify the files as arguments to the tee command:
echo "this is a line" | tee file_1.txt file_2.txt file_3.txtAnother advantage of the tee command is that you can use it in conjunction with sudo
to write to files owned by other users. To write text to a file that you do not have write permissions to, prepend sudo before tee:
echo "this is a line" | sudo tee file.txtThe output of echo is piped to tee, which runs with root privileges and writes the text to the file.
Quick Reference
For a printable quick reference, see the Bash cheatsheet .
| Task | Syntax |
|---|---|
| Write to file (overwrite) | command > file.txt |
| Append to file | command >> file.txt |
| Prevent overwriting | set -o noclobber |
| Override noclobber | command >| file.txt |
| Write multiple lines | cat << EOF > file.txt |
| Write with tee | command | tee file.txt |
| Append with tee | command | tee -a file.txt |
| Write as root | command | sudo tee file.txt |
FAQ
What is the difference between > and >>?
The > operator overwrites the file with the new output. The >> operator appends the output to the end of the existing file without removing its contents.
How do I write to a file without overwriting it?
Use the >> append operator, or enable the noclobber option with set -o noclobber to prevent accidental overwrites when using >.
How do I write to a file that requires root permissions?
Pipe the output to sudo tee: echo "text" | sudo tee /etc/somefile. Using sudo directly with > does not work because the shell handles the redirection before sudo takes effect.
How do I write multiple lines to a file in a script?
Use a heredoc
: cat << EOF > file.txt followed by your lines and a closing EOF delimiter.
Conclusion
For more complex output, pair these redirection patterns with printf
for formatted strings, or with a heredoc
when building multi-line configuration files.
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