How to Append to File in Bash

May 30, 2023

Introduction

Bash is one of the most versatile shell scripting languages that provides users with many data manipulation features. It is versatile and offers a convenient way to append content to files without overwriting old data.

Appending to files is especially useful when working with log or configuration files, where you want to preserve the file history and add new information.

In this tutorial, you will learn various techniques for appending content to files using Bash.

How to append to a file in Bash - a tutorial.

Prerequisites

  • A system running Linux.
  • Access to the terminal/CLI.

Append to File in Bash With >> Redirection Operator

One of the ways to append text to a file in Bash is to use the >> redirection operator.

The >> redirection operator is used in command-line interfaces and shell scripting to control the input and output of commands. Use the operator to redirect a command's output to the end of the specified file. If the file doesn't exist, the system creates it.

The difference between the >> and > operators is that the > operator overwrites any existing contents of the specified file with the command's output. The >> operator only appends the output without overwriting anything.

1. Redirect Output from the echo Command

We will test the >> redirection using the echo command that prints text to standard output. We can redirect that output to append it to a file.

The syntax is:

echo "content to append" >> [file_name]

Replace [file_name] with the file you want to append to. For example:

echo "content to append" >> file.txt

Check if the content is in the file using the cat command:

Appending text to a file in Bash using redirection.

The output shows that the specified content has been appended to the file.

2. Redirect Output from the printf Command

Another command that prints text to standard output is printf. The printf command allows you to format and print text or variables with more control over the output and formatting. That way, you can produce complex outputs and specify the formatting.

The syntax is:

printf "content to append" >> [file_name]

For example:

printf "Hello, you are currently in %s.\n" $PWD >> file.txt
Appending text to a file using printf and redirection.

The command utilizes the output of the printf command and appends it to the specified file, as in the cat command output.

3. Use HereDoc

If you want to append multiple lines of output to a file, use a Here document (HereDoc). HereDocs are useful when redirecting multiple commands at once.

For example, pass the contents using the cat command and append it to a file:

cat << EOF >> file.txt
The current working directory is: $PWD
The current user is: $(whoami)
EOF
Appending text to a file in Bash using HereDoc.

The cat command reads the HereDoc and writes the contents to standard output, which is then redirected to the specified file.

Note: Learn how to open a file in Bash using less command.

Append to File in Bash With tee Command

The Linux tee command is a command-line utility that reads from the standard input and writes to both standard output and one or more files at the same time. Use the command with the -a (--append) option to append the output to a file instead of overwriting it by default.

For example, use the echo command to print something to standard output and pipe the output to the tee command:

echo "new content"  | tee -a file.txt
Appending text to a file in Bash through the tee command.

The advantage of using the tee command instead of the >> redirection operator is that tee allows you to simultaneously append text to multiple files and write to files owned by other users if used with sudo.

To append text to multiple files, specify the files as arguments:

echo "content to append"  | tee -a file1.txt file2.txt file3.txt

The command appends the content to all the specified files.

Conclusion

This tutorial showed how to append text to a file or multiple files in Bash using several methods. Appending to a file in Bash is a straightforward process that can be accomplished using the redirect operator (>>) or the tee command without overwriting previous content.

Next, learn about Bash math operations, or take a look at our Bash HereDoc tutorial with useful examples.

Was this article helpful?
YesNo
Bosko Marijan
Having worked as an educator and content writer, combined with his lifelong passion for all things high-tech, Bosko strives to simplify intricate concepts and make them user-friendly. That has led him to technical writing at PhoenixNAP, where he continues his mission of spreading knowledge.
Next you should read
How to Read Files Line by Line in Bash
March 17, 2022

This tutorial shows how to read a file line by line using the Bash shell. See five different methods and process a file's contents effectively.
Read more
Bash String Comparison
September 22, 2022

This tutorial shows how to use the Bash shell in Linux to compare two strings in different ways. See examples for each comparison and create example scripts.
Read more
Bash printf - How to Print a Variable in Bash
February 24, 2022

The printf command produces formatted text outputs in the terminal. This tutorial shows how to use printf to print and format a variable output in Linux.
Read more
How To Use The Bash read Command
February 21, 2022

The Bash read command reads a user's input or a file descriptor and allows splitting into different fields. Learn how to utilize the command in this guide.
Read more