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.
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:
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
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
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
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.