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

How to Install PostgreSQL on Ubuntu 26.04

PostgreSQL has become one of the most trusted database systems for developers, enterprises, and cloud…

6 hours ago

How to Install Nginx on Ubuntu 26.04

Nginx continues to dominate the modern web hosting world because of its speed, reliability, and…

9 hours ago

PHP Ubuntu 26.04 Installation Guide for Apache and Nginx

Setting up PHP Ubuntu 26.04 is essential for developers who want to run modern web…

12 hours ago

Apache on Ubuntu 26.04 Installation Guide for Beginners

Setting up Apache Ubuntu 26.04 is one of the fastest ways to launch a reliable…

15 hours ago

How to Configure Static IP on Ubuntu Settings Easily

Setting up a Static IP on Ubuntu configuration is essential for servers, remote access systems,…

1 day ago

How to Change Ubuntu Timezone Using Terminal or GUI

Keeping the correct system clock is important for servers, desktop systems, scheduled tasks, and application…

1 day ago