Bash Scripting

How to Bash Append to File: A Simple Guide for Beginners

If you are working with Linux or writing bash scripts, one of the most common tasks you will face is adding text to a file without erasing what’s already inside it. This is called bash append to file. In simple words, it means writing new content at the end of a file instead of overwriting it.

This guide will explain different ways to append text to a file in Bash, with easy-to-understand examples.

What Does Bash Append to File Mean?

Normally, when you use > to send output to a file, it replaces the existing content. But when you use >>, Bash adds the new content at the bottom of the file.

Think of it like adding more notes at the end of a notebook instead of tearing out the old pages.

Method 1: Append with Echo and >>

The easiest way is to use the echo command with the append operator >>.

echo "This is a new line" >> file.txt
  • echo prints the text.
  • >> appends it to the end of the file.
  • If the file doesn’t exist, Bash creates it for you.

You can also use echo -e to add multiple lines:

echo -e "First line\nSecond line" >> file.txt

Method 2: Use printf for Better Formatting

Another way to bash append to file is by using printf. This command is similar to echo but gives you more control over spacing, alignment, and formatting.

printf "Hello, %s!\n" "$USER" >> file.txt

This example adds your username to the file with a new line at the end.

Method 3: Append Multiple Lines with a Heredoc

If you want to add several lines at once, a Heredoc is a cleaner option:

cat << EOF >> file.txt
This is line one
This is line two
This is line three
EOF

Everything written between EOF markers will be appended to the file.

Method 4: Append Using tee Command

The tee command is useful if you want to append and also see the output on your screen:

echo "Another entry" | tee -a file.txt
  • The -a option tells tee to append instead of overwrite.
  • You will see the text on the terminal and it will also be added to the file.

If you do not want the output shown on screen, use:

echo "Another entry" | tee -a file.txt >/dev/null

This is especially handy when you need to use sudo to append text to protected files:

echo "Config line" | sudo tee -a /etc/system.conf

Method 5: Append Command Output

Instead of adding just text, you can append the result of a command. For example:

date >> logfile.txt

This will save the current date and time at the end of the file.

You can also save both normal output and errors:

command >> output.txt 2>&1

This way, both standard output and error messages are added to the same file.

Which Method Should You Use?

Here’s a quick guide:

  • Use echo with >> for simple lines.
  • Use printf when you need proper formatting.
  • Use a Heredoc to add multiple lines at once.
  • Use tee -a when you also want to see the output in the terminal.
  • Use redirection with 2>&1 when you need to capture both output and errors.

Final Thoughts

Knowing how to bash append to file is an essential skill for anyone working with Linux. It helps you log information, update configuration files, or add new content without losing what’s already there.

By practicing the methods above, you will quickly become confident with appending text in Bash. Start with the simple echo >> command and explore the other options as your tasks get more advanced.

0xSnow

0xSnow is a cybersecurity researcher with a focus on both offensive and defensive security. Working with ethical hacking, threat detection, Linux tools, and adversary simulation, 0xSnow explores vulnerabilities, attack chains, and mitigation strategies. Passionate about OSINT, malware analysis, and red/blue team tactics, 0xSnow shares detailed research, technical walkthroughs, and security tool insights to support the infosec community.

Recent Posts

Install VirtualBox on Ubuntu 18.04 from the Oracle Repository

VirtualBox is a free, open-source, cross-platform virtualization application maintained by Oracle. It lets you run multiple…

12 hours ago

Install PostgreSQL on Ubuntu 18.04 with Remote Access Setup

PostgreSQL (also called Postgres) is a free, open-source, object-relational database management system with a strong reputation…

12 hours ago

Install VMware on Ubuntu 18.04: Workstation Player Setup Guide

VMware Workstation Player is a mature, stable virtualization platform that lets you run multiple isolated operating…

12 hours ago

Set Up a UFW Firewall on Ubuntu 18.04: Allow, Deny, and Manage

A properly configured firewall is one of the most important layers of security for any internet-facing server. UFW (Uncomplicated Firewall) is a user-friendly front-end for managing iptables rules that ships pre-installed on Ubuntu. Its defaults are sensible: block all incoming connections, allow all outgoing connections. No outside traffic reaches your server unless you explicitly open a port. This guide covers how to configure a UFW firewall on Ubuntu 18.04, from setting default policies and application profiles to writing allow and deny rules for specific ports, IPs, and subnets. <strong>Prerequisite:</strong>&nbsp;You&nbsp;need&nbsp;sudo&nbsp;access. Configure UFW Firewall on Ubuntu: Defaults, SSH, and Application Profiles If UFW is not installed, add it with: bashsudo…

12 hours ago

Disable UFW Firewall on Ubuntu 18.04: Stop, Reset, and Re-enable

UFW (Uncomplicated Firewall) is a user-friendly front-end for managing iptables rules on Ubuntu. It ships pre-installed…

12 hours ago

Install Apache Cassandra on Ubuntu 18.04: NoSQL Setup Guide

Apache Cassandra is a free, open-source NoSQL database designed for high availability and linear scalability with…

2 days ago