• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
NameHero® Blog

NameHero® Blog

Web Hosting Tips & Resources From NameHero

  • AI Agents
    • OpenClaw
    • Claude Code
    • n8n
    • Hermes Agent
    • Open WebUI
    • Docker
  • Hosting
    • Web Hosting
    • WordPress Hosting
    • WooCommerce Hosting
    • Enterprise Hosting
    • Email Hosting
    • HeroicGuard
    • GPU Hosting
    • Website Builder
  • VPS
    • Managed VPS
    • Unmanaged VPS
    • Flex VPS
  • Reseller
  • Gaming
  • Domains
  • Account
  • Blog Home
  • Categories
  • Authors

How to Append to a File in Bash

Bhagwad Park

Published on: February 26, 2025

Categories: Linux Command Line, VPS Hosting 0

From the point of view of someone who’s not very experienced with Linux, it can seem to be a very niche scenario where you’d want to append something to a file. But as soon as you begin managing a server, you’ll see we have many reasons to add content to a file. As a result, Linux has various tools allowing us to do this, each of which corresponds to a different use case scenario. For example, some tools allow us to write to multiple files at the same time, other syntaxes let us check for write errors before we start appending.

To begin with, let’s look at the basic techniques for appending a file in Linux.

Table of Contents
  • How to Append a File in Bash
    • Basic “echo” Usage to Append
    • Appending Multiple Lines to a File
    • Appending the Output of Commands
    • Appending to Multiple Files at Once
    • Adding Newlines Before Appending
  • Appending Safely to a File
  • Conclusion

How to Append a File in Bash

There are a huge number of ways in which you can append content to a file in bash.

Basic “echo” Usage to Append

The most basic use case for appending a file in bash is like this:

echo "New line of text" >> filename.txt

The above command will append “New line of text” to “filename.txt. If the file doesn’t exist, bash will create it. If it’s not a text file, then the results could vary, but will most likely result in the corruption of the file. For example, if you try and append text to a PNG image file, you won’t be able to open the image properly.

Appending content to other file types can be potentially more dangerous. For example, writing a system device file can have unpredictable system-wide effects. Or you could write to a special location like /dev/shm, in which case your content will be available via RAM to other processes.

Appending Multiple Lines to a File

When you use the echo command to append to a file as above, you can’t press the “Enter” key to write multiple lines. The easiest way to add multiple lines is to use the HEREDOC construct as shown below:

cat <<EOF >> filename.txt
This is line 1
This is line 2
EOF

I’ve written before about the incredible utility of the HEREDOC construct. We can use it here to append multiple lines and not worry about escaping special characters. If you want to know more about HEREDOC, I suggest you check out the tutorial I linked above.

Appending the Output of Commands

Most often, you won’t have the exact text of what you want to append in advance. Instead, the content will come from another command. Here’s how you append the output of a command to a file in bash:

date >> log.txt
ls -l >> filelist.txt

The above command will append the output of the “date” and the “ls” command to the two specified files. Note that the syntax is the same as the one for the “echo” command because, in the latter situation, we’re doing the same thing – appending the output of “echo” and adding it to a file. So you can say that this is a more general case of the first one.

Appending to Multiple Files at Once

All the above examples only allow you to write to a single file at once. Sometimes, you might want to add the same text to a bunch of files at the same time. For this, we use the “tee” command:

echo "Logging to multiple files" | tee -a file1.txt file2.txt

The “tee” command also has other uses, which often makes it superior as an append tool, particularly when you want to see the output while appending the output at the same time. With tee, you can also append to write protected files using sudo. For example:

echo "Privileged entry" | sudo tee -a /root/protected.txt

For example, the following example will NOT work:

sudo echo "Privileged entry" >> /root/protected.txt

Here’s a screenshot:

Writing with tee

As you can see, merely using “sudo” for the echo statement isn’t enough. With the “tee” command, however, you can use sudo and append content to a write-protected file.

Adding Newlines Before Appending

When you append some text to a file in bash, it doesn’t check if the previous line had a new line before it. This can sometimes lead to unexpected formatting. For example, if we have a file:

First line
Second line
Third line (no newline here)

Then appending something to this file will make it this:

First line
Second line
Third line (no newline here)New entry

As you can imagine, this is probably not the expected output. The new entry should logically be on a separate line, given the existing structure of the file. Normally, when bash appends text to a file, it automatically adds a new line to the end of it. So appending further lines works as intended.

If, however, the manner in which the previous lines were added didn’t add a new line to the end of the last line, then appending a line presents you with the problem shown above. If you have something like a log file, then you know that all the previous lines were also appended, so you don’t have to worry. But if you need to check, then you can use the following command to add a new line to the end of the file if one doesn’t already exist:

sed -i -e '$a\' filename.txt

The above command uses sed – a powerful stream editor developed in the 1970s but still widely in use today. In an earlier NameHero tutorial, I explained how to use sed to delete lines. The above command actually exploits a quirk in the way the shell interprets backslashes and quotes – just don’t think about it too hard.

Once you’ve run the above command, you can then append whatever text you want without worrying about newlines.

Appending Safely to a File

You won’t always have permission to append text to a file. If you expect to encounter such situations, you have two options. First, you can just go for it! If it fails, it fails and no harm is done. In any case, you’ll get an error message like “Permission denied” if the bash append command isn’t successful.

The second option is to test for an error before appending. For example, something like this:

if [ -w filename.txt ]; then
echo "Appending safely" >> filename.txt
else
echo "Error: Cannot write to file"
fi

The above script first checks to see if the file is writable, and only then does it proceed with appending the text. If not, it throws an error message. The problem is that this adds another layer of checks, and it’s not clear that there’s any benefit to simply going for it and accepting the failure.

The one exception is where you want the script to stop executing if the append fails. For this, we use:

#!/bin/bash
set -e # Stop script on any error

The above line, when included on a script, causes the script to immediately exit. This can prevent a cascade of errors if the following commands rely on the successful execution of previous ones. It can also prevent your logs from filling up with errors if the script keeps running.

It’s a design decision either way, and there’s no right or wrong answer.

Conclusion

As you can see, there are many ways of appending content to a file in bash. Each of them is useful under different circumstances, so you should be familiar with the differences between them. Sometimes you might need to combine it with commands like “tee” to work with protected files as well.

Bhagwad Park Profile Picture
Bhagwad Park

I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!

Related Posts

Bash Signal Handling with Trap: EXIT, ERR, INT

Bash in Linux allows us to set functions that run when certain signals are generated - either by the script, or the shell. Here's how.

Jq Command: All you Need to Know

This jq command tutorial will walk you through the usage of "jq" - a command that works with JSON files like "awk" works with text.

How to Use the xargs Command in Linux

The "xargs" command converts text from standard input into arguments, allowing the output for one command to be used as arguments in other.

How to Tar and Gzip a Folder

Here's how to archive and compress a folder so that it's easy to move it from one location to another - In both Linux AND Windows!

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow & Subscribe

Exclusive promos, content and more!


Most Popular Posts

NameHero’s Recommended WordPress Plugin and Theme Setup

WordPress Hosting vs. Web Hosting – What’s The Difference?

How To Increase The InnoDB Buffer Pool Size

How To Fix A Stuck All-in-One WP Migration Import

How To Add A Subdomain In Cloudflare

Top Categories

  • WordPress
  • WordPress Tutorials
  • OpenClaw Hosting
  • Enterprise Hosting
  • WooCommerce
  • Web Hosting
  • Resellers
  • Website Security
  • Website Development
  • Website Performance
  • VPS Hosting
  • SEO Tips
  • Announcements
  • Domain Registration
NameHero

NameHero® proudly provides web hosting to over 40,000 customers with 99.9% uptime to over 750,000 websites.

  • Master Card
  • Visa
  • American Express
  • Discover
  • Paypal
Products
  • Web Hosting
  • Managed VPS Hosting
  • Unmanaged VPS Hosting
  • Flex VPS Hosting
  • WordPress Hosting
  • WooCommerce Hosting
  • Reseller Hosting
  • Enterprise Hosting
  • GPU Hosting
  • Email Hosting
  • HeroicGuard
  • Domains
  • Website Builder
  • AI Agent Hosting
Help & Support
  • NameHero Blog
  • NameHero Gaming Blog
  • Support
  • Help Center
  • Migrations
  • Affiliates
  • Gaming Affiliates
  • Call 1-855-984-6263
Company
  • About Us
  • Contact Sales
  • Reviews
  • Uptime
  • We're Hiring

Copyright © 2026 Name Hero, LLC. All rights reserved.
NameHero® is a registered trademark.

  • Privacy Policy
  • Terms of Use
  • Acceptable Use Policy
  • Payment Policy
  • DMCA