Updated: 22 October 2026 · Applies to: Bash 5.2 and 5.3, GNU sed 4.9 and tee on Debian 13, Ubuntu 24.04, Ubuntu 26.04 (uutils tee 0.10.0) and AlmaLinux 10, tested
To append a line to the end of a file in Bash, redirect with >>: echo "new line" >> file.txt. The line is added after the existing content, and the file is created if it does not exist. A single > replaces the whole file, so double-check before you press Enter. For a file owned by root, use echo "new line" | sudo tee -a /etc/app.conf > /dev/null.
echo "new line" >> file.txt # one line printf '%s\n' "line 1" "line 2" >> file.txt # several lines cat >> file.txt <<'EOF' # a block of text first line of the block second line of the block EOF echo "max=10" | sudo tee -a /etc/app.conf > /dev/null # file owned by root sed -i '$a new line' file.txt # with sed
All output below is from our test containers (Debian 13, Bash 5.2, user alice). The same commands gave the same results on Ubuntu 24.04, Ubuntu 26.04 and AlmaLinux 10.
1. Append a line with >>
echo "first line" >> notes.txt echo "second line" >> notes.txt cat notes.txt
Output:
first line second line
With one > the old content is gone:
echo "oops" > notes.txt cat notes.txt
Output:
oops
To protect yourself from that mistake, turn on set -o noclobber (add it to ~/.bashrc to keep it). Bash then refuses > on an existing file, while >> still works. >| overwrites on purpose.
set -o noclobber echo new > notes.txt
Output:
-bash: notes.txt: cannot overwrite existing file
echo more >> notes.txt cat notes.txt
Output:
oops more
2. Check that the file ends with a newline
If the last line of a file has no newline character (common for files saved by some editors or written with printf without \n), the appended text joins that line:
printf 'alpha' > list.txt echo "beta" >> list.txt cat list.txt
Output:
alphabeta
Add the missing newline first. sed -i -e '$a\' FILE adds one only when it is missing and leaves a correct file unchanged:
printf 'alpha' > list.txt sed -i -e '$a\' list.txt echo "beta" >> list.txt cat list.txt
Output:
alpha beta
A version without sed: [ -n "$(tail -c1 list.txt)" ] && echo >> list.txt (it adds an empty line only if the last byte is not a newline).
3. Append several lines
printf prints each argument on its own line with the '%s\n' format and behaves the same in every shell:
printf '%s\n' "line 3" "line 4" >> multi.txt
echo -e 'line 1\nline 2' >> multi.txt also works in Bash, but in sh scripts (dash on Debian and Ubuntu) echo handles \n differently, so printf is the safer choice in scripts. More on echo options in The echo Command in Linux: Print Text and Variables with Examples.
4. Append a block with a here-document
For longer text, such as a configuration block, use cat >> FILE <<EOF. Without quotes around EOF, variables are expanded; with 'EOF' the text is written exactly as typed:
name=web01 cat >> hd.txt <<EOF server=$name home=$HOME EOF cat >> hd.txt <<'EOF' literal=$name EOF
cat hd.txt
Output:
server=web01 home=/home/alice literal=$name
The closing EOF must be alone on its line, with no spaces after it, according to the Bash manual. <<-EOF strips leading tab characters, so you can indent the block inside a script. Here-documents and here strings are explained in Input Redirection in Linux: stdin, Here Documents and Here Strings Explained.
5. Append with tee -a
tee -a appends what it reads to one or more files and also prints it, so you see what was written. Add > /dev/null to hide the copy on screen:
echo "x=1" | tee -a app.conf
Output:
x=1
echo "y=2" | tee -a app.conf > /dev/null echo "z=3" | tee -a app.conf a2.conf
Output:
z=3
cat app.conf
Output:
x=1 y=2 z=3
Without -a, tee overwrites the file, just like >.
6. Append to a file owned by root
This is the most common surprise. sudo echo ... >> FILE fails, because your own shell opens the file for the redirection before sudo runs, and your user may not write to it:
sudo echo "max=10" >> /etc/lab.conf
Output:
-bash: /etc/lab.conf: Permission denied
Use one of these instead:
- Pipe into
sudo tee -a, which opens the file as root:echo "max=10" | sudo tee -a /etc/lab.conf > /dev/null
- Run the whole command, redirection included, in a root shell:
sudo sh -c 'echo "min=1" >> /etc/lab.conf'
- Append a block as root with a here-document:
sudo tee -a /etc/lab.conf > /dev/null <<'EOF' [cache] size=64M EOF
Result in our test:
cat /etc/lab.conf
Output:
# lab file max=10 min=1 [cache] size=64M
Before you change a system file, keep a copy, for example sudo cp -a /etc/lab.conf /etc/lab.conf.bak.
7. Append with sed
GNU sed's a command adds text after a line, and $ means the last line. -i edits the file in place:
printf 'one\ntwo\n' > s.txt sed -i '$a three' s.txt cat s.txt
Output:
one two three
Two things to know:
- On an empty file,
sed '$a ...'adds nothing, because there is no last line. Our test file stayed at 0 bytes. Use>>for files that may be empty. - sed removes leading spaces from the text. To keep them, write a backslash first:
sed -i '$a\ indented line' s.txt.
sed is most useful when the text must go somewhere other than the end, for example after a matching line or at the top:
printf '[main]\nport=80\n[extra]\n' > ini.conf sed -i '/^\[main\]/a user=www' ini.conf sed -i '1i # managed by deploy' ini.conf cat ini.conf
Output:
# managed by deploy [main] user=www port=80 [extra]
More sed options: sed Options and Their Usage.
8. Append a line only if it is not there yet
In scripts that may run more than once, check first so the line is not added twice. grep -qxF looks for the exact whole line:
grep -qxF 'PermitRootLogin no' t.conf || echo 'PermitRootLogin no' >> t.conf
We ran this twice and the file contained the line once. If the file does not exist yet, grep reports an error and the line is still appended, which creates the file.
Other common cases
- Append one file to another:
cat extra.txt >> main.txt. - Append the output of a command, errors included:
command >> report.txt 2>&1. See How to Save Terminal Output to a File in Linux for more on saving output, and How to redirect standard error in bash. - Append a line with a time stamp to a log:
echo "$(date '+%F %T') backup done" >> backup.logadds a line such asYYYY-MM-DD HH:MM:SS backup done.
Official documentation: Bash manual, Redirections and GNU sed manual.