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

CVE-Search : A Tool To Perform Local Searches For Known Vulnerabilities

Introduction When it comes to cybersecurity, speed and privacy are critical. Public vulnerability databases like…

3 minutes ago

Mastering the Bash Case Statement with Simple Examples

What is a bash case statement? A bash case statement is a way to control…

33 minutes ago

How to Check if a File Exists in Bash – Simply Explained

Why Do We Check Files in Bash? When writing a Bash script, you often work…

2 days ago

Bash Arrays Explained Simply: Beginner’s Guide with Examples

If you’re learning Bash scripting, one of the most useful features you’ll come across is…

3 days ago

Bash For Loop Examples Explained Simply for Beginners

If you are new to Bash scripting or Linux shell scripting, one of the most…

3 days ago

How Does a Firewall Work Step by Step

How Does a Firewall Work Step by Step? What Is a Firewall and How Does…

5 days ago