bash append to file
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.
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.
>>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.You can also use echo -e to add multiple lines:
echo -e "First line\nSecond line" >> file.txt 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.
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.
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 -a option tells tee to append instead of overwrite.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 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.
Here’s a quick guide:
echo with >> for simple lines.printf when you need proper formatting.tee -a when you also want to see the output in the terminal.2>&1 when you need to capture both output and errors.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.
General Working of a Web Application Firewall (WAF) A Web Application Firewall (WAF) acts as…
How to Send POST Requests Using curl in Linux If you work with APIs, servers,…
If you are a Linux user, you have probably seen commands like chmod 777 while…
Vim and Vi are among the most powerful text editors in the Linux world. They…
Working with compressed files is a common task for any Linux user. Whether you are…
In the digital era, an email address can reveal much more than just a contact…