Bash append to file with >> (the append redirect). The single > overwrites the file; >> adds to the end. The three common patterns: echo line >> file (single line), tee -a file (when you also want stdout), and cat <<EOF >> file (multiline). The team that scripts log lines or config fragments uses these daily.
Table of contents
- The >> operator
- tee -a for simultaneous stdout and append
- Multiline append with heredoc
- printf for portable single-line appends
- How this fits the rest of the stack
- FAQ
The >> operator
Append a single line:
echo "deploy: $(date)" >> /var/log/deploy.log
The >> opens the file for append, writes the line, and closes. The file is created if it does not exist. The team that scripts a log line uses this pattern constantly.
Append a command’s output:
ls -la >> /tmp/snapshot.txt
The >> does not add a newline if the file does not end with one - if the last line of the file is foo and the command outputs bar, the file becomes foobar on the last line. The team that hits this bug adds an explicit echo "" or uses tee.
tee -a for simultaneous stdout and append
echo "building..." | tee -a /var/log/build.log
tee reads stdin, writes to stdout AND to the file. -a means append (default is overwrite). The team that wants to see the output in the terminal AND save it to a log file uses this.
Multiple files: echo "status: ok" | tee -a /var/log/app.log /var/log/mirror.log. The team that has redundant log destinations (a local file and a sidecar) uses the multi-file form.
Multiline append with heredoc
cat <<'EOF' >> /etc/nginx/conf.d/myapp.conf
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://127.0.0.1:3000;
}
}
EOF
The <<'EOF' (with quotes) tells the shell to not expand variables or backticks inside the heredoc - the content is literal. The team that writes config snippets uses this. The >> file redirect appends the whole block to the file.
Without quotes (<<EOF), the shell expands $variables and backticks inside the heredoc. The team that wants to interpolate a value (like a version number) drops the quotes. The team that accidentally interpolates a $ they meant to keep hits a hard-to-debug bug.
printf for portable single-line appends
printf '%s\n' "$line" >> /var/log/app.log
More portable than echo across shells. The team that scripts for both bash and sh uses printf - some POSIX sh implementations do not handle echo -e consistently.
FAQ
What is the difference between > and >>?
> overwrites the file with the new content. >> appends to the end, preserving what was already there. The team that uses > instead of >> by accident loses the file’s prior content - that is the most common bash bug.
How do I append to multiple files at once?
Use tee -a file1 file2 file3. Or chain the append: cmd >> file1 && cmd >> file2. The team that has a multi-file log destination uses tee -a.
How do I append without a trailing newline?
printf does not add a newline by default: printf 'no newline' >> file. echo always adds one (unless you use echo -n in bash).
Why is the file empty after my append?
Usually the command produced no output. Check with cmd > /dev/stderr first to verify the command actually writes something. The team that has a cmd that writes to a log file directly (not stdout) needs to redirect the file’s fd separately.
Can I append as a different user?
Use sudo tee -a /path/to/file < input and enter the password when prompted. sudo echo "..." >> /etc/... would also work because the >> runs as root in that case, but tee is the more general pattern for non-trivial input.
How this fits the rest of the stack
For a sense of what the full project costs before it commits, the RunxBuild hosting calculator shows the line items together. The API, the database, the storage, the worker, the bandwidth - each one is a separate number, and the team’s mental model for the platform is the sum of those numbers.
Useful related references: