
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.
In this article, you will explore multiple techniques to append 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.
The Short Answer Version
If you’re already familiar with Bash and want a quick reference, use the following examples:
# Append a single line to a file
$ echo "Line of text" >> file.txt
# Append output of a command
$ date >> log.txt
# Append both stdout and stderr
$ some-command >> output.txt 2>&1
# Append and display output with tee
$ echo "Log entry" | tee -a file.txt
# Append inside a loop
$ for i in {1..3}; do echo "Step $i" >> loop.txt; done
Use the >>
Operator to Append Text
The simplest way to append text in Bash is by using the >>
redirection operator.
$ echo "A Bash file" >> output.txt
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.
Append Output of a Command
To append the output of a Bash command to a file, use the >>
redirection operator.
$ date >> system_log.txt
This appends the current date and time to system_log.txt
.
To append both standard output and standard error, use:
$ ls /bashfile >> error_log.txt 2>&1
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
.
Use tee -a
to Append and Display Output
The tee
command with the -a
flag appends output to a file while also displaying it in the terminal.
$ echo "Log entry" | tee -a log.txt
This appends "Log entry"
to log.txt
and simultaneously prints it to the terminal. This is useful for debugging scripts or real-time log inspection.
Append Using A for Loop
Use loops to append multiple lines or structured entries to a file.
for i in {1..3}; do
echo "Loop iteration $i" >> loop_log.txt;
done
This loop runs three times and appends one line per iteration to loop_log.txt
.
Append Multi-line Text Blocks
To append multiple lines of text at once, use a here document syntax with the cat
command and the >>
redirection operator.
cat <<EOF >> settings.conf
# Application Settings
port=3000
enable_feature=true
EOF
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.
Shell Script Example for Appending
The following Bash script demonstrates several common append patterns, including static text, loops, and multi-line blocks.
#!/bin/bash
log_file="demo_log.txt"
# Append a timestamp
echo "Script started at $(date)" >> "$log_file"
# Append entries in a loop
for i in {1..5}; do
echo "Logging step $i" >> "$log_file"
done
# Append a multi-line footer
cat <<EOF >> "$log_file"
---
End of Script
---
EOF
Save the file as append_demo.sh
, then make it executable and run it:
$ chmod +x append_demo.sh
$ ./append_demo.sh
After the script finishes, view the contents of the log file.
$ cat demo_log.txt
Output:
Script started at Mon Jun 17 22:41:01 IST 2025
Logging step 1
Logging step 2
Logging step 3
Logging step 4
Logging step 5
---
End of Script
---
Check File Permissions Before Appending
To avoid write errors when appending, verify that the file is writable.
$ [ -w demo_log.txt ] && echo "Writable" || echo "Not writable"
This checks whether your current user or script has write permissions to the file before attempting to append.
Conclusion
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.
No comments yet.