Use printf and >>:
printf '%s\n' "new line of text" >> app.log
echo "text" >> app.log works too and is fine for a throwaway command. It is not fine in a script, and this page runs the same echo command through four implementations on one machine and gets three different answers. It also measures what >> actually guarantees when two processes write at once — which turns out to be a great deal.
For appending whole files and multi-line blocks with cat and heredocs, see appending to a file with the cat command. This page is about the operator and the string.
Table of Contents
> destroys, >> adds — and the shell does it first
$ echo "line1" >> fresh.txt
$ echo "line2" >> fresh.txt
after two >>: 2 lines
$ echo "line3" > fresh.txt
after one > : 1 lines, content=line3
Both create the file if it does not exist. The distinction only bites on the second run, and it bites silently: exit status 0, no warning, previous contents gone.
Bash has a guard for this, and it is worth knowing that the guard is one-sided:
$ bash -c 'set -o noclobber; echo x > fresh.txt'
bash: line 1: fresh.txt: cannot overwrite existing file
> exit=1
$ bash -c 'set -o noclobber; echo x >> fresh.txt'
>> exit=0
noclobber blocks > onto an existing file and leaves >> alone, which is exactly the right shape. >| overrides it for the one command where you do mean to truncate. It applies per shell, so put set -o noclobber in a script’s header, not in your .bashrc and then assume scripts inherit it.
Four echo implementations, three different answers, one machine
This is the argument for printf, and it is not hypothetical. Running echo -e 'a\tb' through four echo implementations that all exist on this laptop right now:
shell/echo | output of: echo -e 'a\tb'
bash builtin | a b
zsh builtin | a b
/bin/sh builtin | -e a b
/bin/echo (BSD) | -e a\tb
Two of them printed the flag -e as literal text into your file. Now the same strings with no -e at all:
bash builtin | a\tb
zsh builtin | a b
/bin/sh builtin | a b
/bin/echo (BSD) | a\tb
zsh and /bin/sh expanded \t without being asked; bash and /bin/echo did not. So there is no way to write an echo that behaves the same in a #!/bin/bash script, the same script run as sh script.sh, and the same line pasted into a zsh prompt. zsh has been the default login shell on macOS since Catalina, so “it worked in my terminal” and “it worked in my script” are now genuinely different claims.
-n is worse, because it fails by adding bytes rather than by looking wrong. Byte count of echo -n hello, which should be 5:
bash 5 bytes
zsh 5 bytes
/bin/sh 9 bytes
/bin/echo 5 bytes
Nine. macOS’s /bin/sh is bash 3.2.57 in POSIX mode, where echo does not accept -n — so it wrote the literal string -n hello plus a newline. A script that builds a string with echo -n in a loop produces a file full of -n under #!/bin/sh and a correct file under #!/bin/bash.
printf has none of this. Same three shells, same command:
$ printf 'a\tb\n'
bash a b
zsh a b
/bin/sh a b
$ printf '%s' hello | wc -c
bash 5 bytes
zsh 5 bytes
/bin/sh 5 bytes
One habit makes it safe: put the data in an argument, never in the format string.
$ msg='100% done'
$ printf "$msg\n"
100 0one
$ printf '%s\n' "$msg"
100% done
% and \ inside your data are format directives if the data is the format. This is the same class of bug as a format-string vulnerability in C, and it is one of the few ways a log line can silently corrupt itself.
>> means O_APPEND, and that is a real concurrency guarantee
>> opens the file with the O_APPEND flag, which makes “seek to the end” and “write” a single atomic step in the kernel rather than two operations another process can interleave with. That is not a stylistic detail; it is the reason a dozen scripts can append to one log without a lock. There is no matching operator for the other end of the file: prepending a line means rewriting the file, with all the truncation risk that implies.
Two concurrent writers, 5000 lines each, into one file:
A) >> per line: lines=10000 expected=10000
A-lines=5000 B-lines=5000 malformed=0
C) one O_APPEND fd held open per writer:
lines=10000 expected=10000 malformed=0
Nothing lost, nothing interleaved mid-line. Now the identical test with the only change being that each writer holds a normal read-write descriptor instead of an appending one (exec 3<> file rather than exec 3>> file):
B) non-append fd: lines=5000 expected=10000
A-lines=2368 B-lines=2632
Half the data was destroyed. Each process tracked its own file offset, so they wrote over each other from byte zero onwards. The split — 2368 and 2632 — is whichever writer happened to land last on each region, and it will differ on every run. Run A and run C differ from B only in O_APPEND; that flag is doing all the work.
The guarantee is not unlimited. Per the POSIX specification of write(), the offset update and the write are atomic — no intervening file modification operation may occur between them — but a single write() larger than the pipe/filesystem atomicity limit can still be split, and appending to a file over NFS has never been reliable. For ordinary log lines on a local filesystem, >> is safe and you do not need flock.
Appending command output
date >> example.txt
df -h >> system_report.txt
printf '%s started at %s\n' "$0" "$(date -Iseconds)" >> script.log
some_command >> run.log 2>&1 # stdout AND stderr
2>&1 must come after the redirect, not before. Reversing them leaves stderr pointed at the terminal and captures nothing:
$ bash -c 'ls /nonexistent-xyz >> r1.log 2>&1'; cat r1.log
ls: /nonexistent-xyz: No such file or directory
$ bash -c 'ls /nonexistent-xyz 2>&1 >> r2.log'
ls: /nonexistent-xyz: No such file or directory <- on the terminal
$ cat r2.log
<- empty
What was and was not tested
Every figure above is output from a command run on macOS 26.6 (arm64) against GNU bash 5.3.15, zsh 5.9, and /bin/sh (bash 3.2.57 in POSIX mode). The concurrency numbers came from a 5000-line two-writer test run once each; the 10000/10000 and 5000/10000 results are reproducible, but the 2368/2632 split is a single sample of a race and will not repeat exactly. append-strings-to-file-bash.sh holds the six groups of checks behind this page (>> versus >, noclobber, the echo table, the printf format trap, redirect order and the O_APPEND race), prints what your own shells do with echo -e 'a\tb', and deletes its temporary directory when it exits.
Not tested here: dash, BusyBox ash, and the GNU coreutils /bin/echo that Linux ships — Linux’s /bin/echo supports -e where the BSD one above does not, which adds a fifth behaviour rather than removing one. The O_APPEND guarantee itself is specified in POSIX and this test confirms it on APFS only; it was not run over NFS.