How to Append to File in Bash?

Appending the data into a file means adding the data to the file without changing the text already present in that file rather than overwriting the existing data or creating a new file. This is useful when adding new information to a file without changing or deleting the existing data.

Several approaches are used to append the data into a file. These will be discussed throughout this guide with this timeline:

Note: This guide will use the “testfile.txt” for appending the data, and the “append.sh” file for adding the bash script.

Method 1: Append to File Using Redirect Operator

The redirect operator is a commonly used method to append or replace the content in a file. Two redirect operators (>>) are used to append the data into a file, while a single redirect operator (>) is used to replace the data in a file. In this section, we will discuss the different uses of the redirect operator to append the data into a file.

Example 1: Append a Single Line to File

We can append a single line of data using the echo command in the bash shell script with the >> operator. For instance, to append the text in “testfile.txt”, use the below script:

#!/bin/bash
echo "Linux is best CLI operating system" >> testfile.txt

For executing the script and checking (cat) the text is appended the below commands are used:

$ bash append.sh
$ cat testfile.txt

The output shows that text is appended to the file.

Example 2: Append multiple Single Lines to File

We can append several lines simultaneously in a file by using the echo with the “e” option, and the “\n” is used to add the text on a separate line. For instance, the below code will append two lines in the “testfile.txt” using this bash script:

#!/bin/bash
echo -e "Linux is UNIX-based OS.\nLinux is best CLI operating system" >> testfile.txt

For executing and checking the output in a file, use the below-written commands:

$ bash append.sh
$ cat testfile.txt

The output shows that two lines are added in the “testfile.txt”.

Example 3: Append Commands Output to File

We can directly append the command’s output into a file. For instance, the below bash shell script code will append the “date”, and “ls” commands output into the “testfile.txt”:

#!/bin/bash
echo "Today is: $(date)" >> testfile.txt
echo "File in the directory are: $(ls)" >> testfile.txt

Now, to run and verify the appended data in the file, the below commands are executed in the terminal:

$ bash append.sh
$ cat testfile.txt

Both command’s outputs are appended to the “testfile.txt” as seen in the output.

Example 4: Append Error to File

In the previous example, we appended the output into a file. Similarly, the error can be appended to a file. To append the error into “testfile.txt”, the error numeric descriptor “2” is used as shown in the below bash script:

#!/bin/bash
$(apt update) 2>> testfile.txt

Let’s run and show the output of the file with these commands:

$ bash append.sh
$ cat testfile.txt

The output is appended to the file.

Example 5: Append Output and Error to File

We can append both the output and error to a single file using the below script:

#!/bin/bash
(ls && cd ~/Hello) >> testfile.txt 2>&1

For executing and checking the appended data use following commands:

$ bash append.sh
$ cat testfile.txt

The output verifies that the output and the error are appended to the file.

Example 6: Append Command Line User Input to File

To make the bash script for appending the data into the file interactive, we can use the read command to append the user’s input. The below code will take the user input and append it to the “testfile.txt”:

#!/bin/bash
read -p "Enter text: " text
echo $text >> testfile.txt

Let’s run and display the appended file data using these commands:

$ bash append.sh
$ cat testfile.txt

The output shows that the user input is added to “testfile.txt”.

Method 2: Append to File Using cat Method

The cat short of concatenate command is used to append the user input data into a file. The below bash script will append the user input into “testfile.txt”:

#!/bin/bash
cat >> testfile.txt

To run the above bash shell script, utilize this command:

$ bash append.sh

Let’s check the “testfile.txt” to verify the data is appended by executing this command:

$ cat testfile.txt

The below syntax of the cat command, known as the heredoc method, can also be utilized to append the data into a file. The heredoc method uses the “cat << some-word >> filename” to start the body for executable commands and ends with writing the “some-word” at the end.

For instance, to execute two commands (USERNAME and HOSTNAME) using the heredoc method following bash code is utilized: 

#!/bin/bash
cat << COMMAND >> testfile.txt
My user name is: $USERNAME
My host name is: $HOSTNAME
COMMAND

Let’s run the script and display the appended text of the file using these commands:

$ bash append.sh
$ cat testfile.txt

The output displays that both command outputs are appended to the “testfile.txt” with the help of the cat command.

Method 3: Append to File Using tee Command

The ”tee” command is used to copy the standard input to a file. The tee command ”a” option is used to append the standard input to a specific file. For instance, the below script appends the text to the ”testfile.txt”:

#!/bin/bash
echo "Hello World!" | tee -a testfile.txt

For running the above shell script and showing the ”testfile.txt” content, the below-written commands are utilized:

$ bash append.sh
$ cat testfile.txt

The output verifies that the data is appended using the tee command.

That’s all about appending a file in Bash.

Conclusion

Three methods are used to append the data to the bash file in Linux: the redirect operator (>>), cat, and tee commands. Moreover, as explained in this guide, we can use these methods to append a single line or multiple lines of data and append output & error into a file.