
Appending text to files is a fundamental task in Bash scripting. Whether you're logging events, capturing command output, or appending structured data inside loops, Bash makes it easy to add content without overwriting existing files. This ability is particularly useful in automation, reporting, system maintenance, and continuous logging tasks.
This article covers multiple techniques for appending content to a file using Bash. These include simple redirection with >>, command output appending, using tee -a for dual output, multi-line text blocks with cat, appending within loops, and complete Bash script examples.
If you’re already familiar with Bash and want a quick reference, use the following examples:
>> Operator to Append TextThe simplest way to append text in Bash is using the >> redirection operator.
This command appends the line "A Bash file" to output.txt. If the file doesn’t exist, Bash creates it. If it does exist, the text is added to the end of the file without modifying existing content.
To append the output of a Bash command to a file, use the >> redirection operator.
This appends the current date and time to system_log.txt.
To append both standard output and standard error, use:
This attempts to list a nonexistent directory /bashfile. Since the command fails, it produces an error. The 2>&1 syntax redirects standard error (2) to the same destination as standard output (1), appending both to error_log.txt.
tee -a to Append and Display OutputThe tee command with the -a flag appends output to a file and displays it in the terminal.
This appends "Log entry" to log.txt and prints it to the terminal simultaneously. This is useful for debugging scripts or real-time log inspection.
Use loops to append multiple lines or structured entries to a file.
This loop runs three times and appends one line per iteration to loop_log.txt.
To simultaneously append multiple lines of text, use a here document syntax with the cat command and the >> redirection operator.
A here document (<<EOF) tells Bash to read input from the lines that follow until it reaches the closing delimiter (EOF). The contents between <<EOF and the final EOF are treated as literal input and passed to the cat command, which appends them to the file specified.
You can replace EOF with any custom delimiter, like END or CONFIG, as long as the opening and closing markers match exactly.
Create and edit a Bash script file append_demo.sh.
Add the following code to the file. This Bash script demonstrates several common append patterns, including static text, loops, and multi-line blocks.
Save and close the file.
Make the script executable.
Run the script.
After the script finishes, view the log file's contents.
Output:
To avoid write errors when appending, verify that the file is writable.
This checks whether your current user or script has write permissions to the file before attempting to append.
Avoid appending to sensitive files like system logs or configuration files unless absolutely necessary. Improper or unchecked input can result in data loss, corrupted configurations, or security vulnerabilities.
In this article, you learned how to append text to files in Bash using a variety of techniques, including the >> operator, tee, loops, and here documents. These approaches allow you to log information, automate reporting, and modify files dynamically in your shell scripts. With these tools, you can build more resilient and informative Bash workflows that support persistent and organized output across many common scripting use cases.
0 Comments
Be the first to comment and share your perspective with the community.