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:
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:
Next, to use “>>” directional operator for adding any data in the file I have used the above-mentioned syntax like this:
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:
For instance, if I want to add any data to any file the above-mentioned syntax can be used like this:
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:
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:
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:
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:
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:
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:
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:
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.