BASH Programming

How to Write to a File in Bash

Reading and writing the files is one of the common tasks while writing bash scripts. For example, saving the output of a command to a file or simply manipulating the files in bash different commands are used. There are a number of ways of writing any file in bash through terminal and if you are finding ways to write any file while scripting then read this guide.

Writing File in Bash

As mentioned above there are multiple ways of writing any file while bash scripting and below are some ways to write a file in bash:

Using Directional Operators

There are generally two directional operators one can use to write a file while bash scripting, one is single angle bracket “>” and the other is double angle bracket “>>”. The primary difference between the two is that “>” overwrites the data that was previously present in the file with the new data, whereas “>>” on the other hand just adds the new data in the file.

To use any of the directional operators below is the respective syntax that one should follow:

$ echo <“data”> <directional-operator> <filename>

For instance, to add data to any file which is blank currently or to overwrite the data in any file then use the above-mentioned syntax like this:

$ echo “Hello Linuxhint 1> myfile.txt

Next, to use “>>” directional operator for adding any data in the file I have used the above-mentioned syntax like this:

$ echo “Hello Linuxhint 2>> myfile.txt

Remember that the above-mentioned syntax not only writes the file but will also create a file if there isn’t any.

Using tee Command

Another way to write any file while bash scripting is by using the tee command and below is the given syntax for using this command:

$ echo “data” | tee <filename>

For instance, if I want to add any data to any file the above-mentioned syntax can be used like this:

$ echo “Welcome to Linuxhint” | tee myfile.txt

Remember that the above syntax used is beneficial not only for writing the data but it also can be used in case if anyone wants to overwrite the data that is currently present in the file with the new data.

So, if you want to keep the current data and want to write the file with new set of data then use -a flag with the given syntax like:

$ echo “greetings” | tee -a myfile.txt

If you want to add same data to multiple files, then this command can be of great help, the tee command can be used for such purpose like this:

$ echo “greetings” | tee -a myfile1.txt myfile2.txt myfile3.txt

Another benefit of using this command is that one can edit a file that is owned by the other users with the help of using admin privileges like this:

$ echo “This is a test file| sudo tee -a myfile1.txt

Using printf Command

Since the above-mentioned ways do not give the liberty to user to add formatting to the data so if you want to add specific formatting to the data then printf can be used like this:

$ printf “Greetings from linuxhint \nThis is a test file.\n” >> myfile1.txt

Using Heredoc

Another way to write a file in bash is by using the here document format, it’s not any sort of command but it’s more like a format one can follow to write multiple lines of data, below is the format of heredoc:

cat << [delimiter]

data…………

[delimiter]

Here in the syntax cat is used to read the data and delimiter is a sort of boundary for the data; it could be a string or file name. In normal practice usually END or EOF is used as a delimiter, but it all depends on the user’s preference.

For instance, I have created a .sh file and used the format of heredoc like this:

#! /bin/bash

cat << MYFILE

Greetings From Linuxhint

This is a test file created using heredoc

MYFILE

Now to see if the data is saved properly let us run this file using:

$ bash mybashscript.sh

Conclusion

Writing files while bash scripting in a Linux system is a tough job if one does not have sound knowledge of using Linux commands. This guide gives 4 ways to write any file while bash scripting that are: using directional operators, tee command, printf and heredoc.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.