There are various ways to append text to a file in bash. In computing, append means to add something to the end of the file.
In this tutorial, you will learn to append text or content to a file using the redirection operator
>> and the
Table of Contents
- Use the >> Redirection Operator to Append to File in Bash
- Use the tee Command to Append to File in Bash
Use the >> Redirection Operator to Append to File in Bash
In Linux, the symbol
>> is an operator that performs output redirection. The other type of redirection is input redirection.
>> output redirection operator, you can append to a file that does not exist or without overriding the contents of the existing file. The tutorial will demonstrate a few ways of appending text to a file in the sub-sections below.
Use the >> Redirection Operator with the echo Command
You can redirect an output produced by the echo command using the >> operator as follows.
echo "This is the first line" >>test.txt
The command above will create a file test.txt. Next, write the command cat test.txt in the shell to display the file’s contents.
This is the first line
Here, any empty file test.txt is created, and some text is appended to the file.
Use the >> Redirection Operator with the printf Command
You can also use the printf command to print the output and redirect the output with the >> operator. The example is shown below.
#!/bin/bash lineNumber="second" printf "This is the %s line\nThis is the third line\n" "$lineNumber" >>test.txt
Next, use the cat command.
This is the first line This is the second line This is the third line
In the example above, you appended the new texts to the same file, test.txt. As a result, the new lines are added after the test.txt file content.
The use of the
\n character forces the text to a new line. As a result, the output is redirected into two lines.
The key takeaways from this example are summed up below:
- The new output is appended in the same file, test.txt.
- You can use the format specifier while appending texts using the
- You can append multiple lines of text with the use of the
Append Multiple Lines of Text with the echo -e Command
Apart from the
printf command, you can also use the
echo command with the
-e option to output the multiple lines. The
-e option in the echo command lets you use the
\n escape sequence.
echo -e command and display some output using the
\n character. Next, append the output to a myFile.txt file.
The example is shown below.
echo -e "This is line number one\nThis is line number two" >>myFile.txt
Then, use the cat command.
This is line number one This is line number two
Here, the escape sequence
\n forces a new line. Thus, the output above is appended in two lines.
Append Multiple Lines of Text using Heredoc
Here document, which is also called Heredoc, is an output redirection used in Linux shells. You can write multiple lines of texts using the Heredoc in the shell. Using it with the
cat command will let you append multiple lines to a file.
You need to use a delimiter at the beginning and at the end of the command to specify the contents to be appended to the file.
Look at the example below.
cat << EOF >> countries.txt Germany Austria Mexico EOF
In the example above, EOF is used as the delimiter. The delimiter can be anything.
Germany Austria Mexico
Append the Output of a Command to the File
You can also redirect the output of any command to a file in bash. The syntax for the operation is as follows.
For example, you can append the output of the command uptime to a file info.txt as follows.
The above commands create a file info.txt containing the following contents.
22:15:21 up 4:28, 1 user, load average: 3.71, 3.90, 4.08
Append a File to Another File
You can even append a file to another using the cat command in bash. The syntax for the operation is:
cat file1 >> file2
Here, the content of file1 is appended to the last of file2.
Consider the files info.txt and countries.txt that you created in the examples above. Write the cat command and append the info.txt to the file countries.txt as follows:
cat info.txt >> countries.txt
You can see the content of the countries.txt file using the cat command.
Germany Austria Mexico 22:15:37 up 4:29, 1 user, load average: 3.53, 3.85, 4.0
Initially, the countries.txt file contained only the list of three countries, and after appending the content of info.txt, the uptime is added to the last.
Use the tee Command to Append to File in Bash
Another method of appending text to a file is using the
tee command. You can use the tee command through piping while using the commands like
printf. The command reads the output and appends it simultaneously to the standard output or one or more files.
Use the tee -a Command with the echo Command
-a option with the
tee command will append the output to the file instead of overwriting.
An example is shown below.
echo "the first line" | tee -a journal.txt
The following output is seen in the standard output after the above command executes.
the first line
A new file, journal.txt, is also created with the same content.
If you do not want the output to be seen in the standard output, you can redirect the output to /dev/null as follows:
echo "the first line" | tee -a journal.txt >/dev/null
Use the tee Command to Append Text to Multiple Files
The advantage of tee over
>> is that you can simultaneously append text to multiple files. You can separate the files by whitespace, and it does the job.
echo "this is appended to three files" | tee -a abc.txt def.txt xyz.txt
It creates three files abc.txt, def.txt, and xyz.txt. The following text is appended to these three files simultaneously and in the standard output.
this is appended to three files
In summary, you learned how to append text to a file in bash using the
>> operator and the tee command.