Skip to content

Bash: Append to File

On this page

To append to a file means to add new content to the end of an existing file, rather than overwriting the file’s existing content. When you append to a file, the new content is added to the end of the file, and the file’s original content is not modified or deleted. This can be useful when you want to add additional information to a file without losing the existing data.

In Bash, there are several ways to append data to the end of a file. Here are two common methods that you can use:

Method 1: Using the “echo” command

The “echo” command is a built-in Bash command that is used to display text to the terminal. It can also be used to write text to a file. To append text to a file using the “echo” command, you can use the “>>” operator to redirect the output of the “echo” command to the file. For example, to append the text “Hello, world!” to the file “hello.txt”, you can use the following command:

echo "Hello, world!" >> hello.txt 

This will add the text “Hello, world!” to the end of the file “hello.txt”, creating the file if it does not already exist. You can also use the “echo” command to append the contents of a variable to a file. For example:

message="Hello, world!" 
echo $message >> hello.txt 

Method 2: Using the “tee” command

The “tee” command is another built-in Bash command that is used to redirect the output of a command to both the terminal and a file. To append data to a file using the “tee” command, you can use the “-a” option to tell “tee” to append the output to the end of the file, rather than overwriting the file.

For example, to append the text “Hello, world!” to the file “hello.txt”, you can use the following command:

echo "Hello, world!" | tee -a hello.txt 

This will append the text “Hello, world!” to the end of the file “hello.txt”, creating the file if it does not already exist. You can also use the “tee” command to append the output of a command to a file. For example:

ls -l | tee -a file.txt 

This will append the output of the “ls -l” command (a list of the files and directories in the current directory) to the file “file.txt”.

One advantage of using the “tee” command to append to a file is that it allows you to preview the output before it is written to the file. This can be useful for debugging or for verifying the output before it is written to the file.

Conclusion

In summary, the “echo” and “tee” commands are both useful for appending data to a file in Bash. The “echo” command is simple and easy to use, while the “tee” command allows you to preview the output before it is written to the file.

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top