How to Write to File in Bash?

The Linux and Unix-based operating systems use the command line methods to write the content into a bash file. Writing to a file in bash is not easy for beginners, but this guide will take you through all the methods to write the content to a file. This guide explains the methods to overwrite the data and append the data in a file using the bash script.

The methods below will help us understand this topic:

Method 1: Using Redirect Operators

The most used method for writing the content into a file is the redirect operator, and it can also be used with other commands. There are two different ways to use the redirect operators; the first one is the single redirect operator (>), which overwrites the data in a file. While the second one is a double redirect operator (>>), which is used to append the data to the file.

Note: The redirect methods and the commands that use the redirector operator for writing or appending the data in a file create the file of the specified file do not exist.

To overwrite the previous data of the bash file, we can use the (>) redirect operator. For instance, to write the “ls” command output to a file named “datafile.txt”, we can use the below command:

Note: The cat command is used for displaying the datafile.txt content.

#!/bin/bash
File=datafile.txt
ls > $File

Let’s discuss the above code line-by-line:

  • #!/bin/bash: It shows that the bash shell script starts here.
  • File: A variable is declared having the filename that will be used.
  • ls: It redirects the ls command output file variable.

Note: The above script is saved in the bash filename named “file.sh”, which will be used throughout this article.

Execute the bash script file named “file.sh” with the following command:

$ bash file.sh

The output shows that the single redirect operator (>) overwrites the data in the “datafiel.txt” text file, which means the already existing file’s content is deleted.

The double redirect operator is used to “append” the data to a file in bash. For example, to append the output of the “pwd” command to “datafile.txt”, the below redirect operator syntax is used in the terminal:

#!/bin/bash
File=datafile.txt
pwd >> $File

The above script will append the pwd command output to the file.

Execute the bash script file named “file.sh” with the following command:

$ bash file.sh

The output shows that the pwd command output is written with the previous file’s content.

Method 2: Using echo Command

The echo command can be used with the redirect operator to write the data into a bash file, and it also creates the file in case the file is not already present. To overwrite the file’s content with the echo command, use the below bash script:

#!/bin/bash
File=datafile.txt
echo "Welcome to itslinuxfoss" > $File

Execute the bash script file named “file.sh” with the following command:

$ bash file.sh

Similarly, to append the data into a bash file, we can use the echo command with the redirect operator (>>). For example, to write the data to “datafile.txt” named text file, run this command:

#!/bin/bash
File=datafile.txt
echo "Welcome to itslinuxfoss" >> $File

Execute the bash script file named “file.sh” with the following command:

$ bash file.sh

Method 3: Using cat Command (Heredoc Format)

The cat command concatenates the file, which can be used to display, create and write the content to the file. To write the data in a bash file, use the below cat command code:

#!/bin/bash
File=datafile.txt
cat testfile.txt >> $File

Execute the bash script file named “file.sh” with the following command:

$ bash file.sh

We can add multiple lines with the cat command heredoc method. Multiple lines of text can be defined inside a delimiter (in this case, FileText) to write them inside a file using bash shell script. For example, the below code writes two lines of data into a text file “dataFile.txt” using the bash script:

#!/bin/bash
FILE=dataFile.txt
cat << FileText >> $File
Welcome to itslinuxfoss
The Linux best website.
FileText

Execute the bash script file named “file.sh” with the following command:

$ bash file.sh

Two lines of data are written (appended) to the file.

Method 4: Using printf Command

The printf command can be utilized to write the content in a file using the bash shell script. For instance, to write a single line into a file “dataFile.txt”, use the below script in bash:

#!/bin/sh
FILE=datafile.txt
printf "Welcome to itslinuxfoss\n" >> $FILE

Execute the bash script file named “file.sh” with the following command:

$ bash file.sh

We can write several lines of data using the printf command with the desired format. For instance, if we want to write 3 lines of data to a file on new lines, use the “\n” with the printf command shown below:

#!/bin/sh
FILE=datafile.txt
printf "Welcome to itslinuxfoss.\nThe best Linux website.\nThank you" >> $FILE

Execute the bash script file named “file.sh” with the following command:

$ bash file.sh

Three lines are written in a file using the bash script.

Method 5: Using tee Command

With the tee command, the output is displayed at the standard output while simultaneously being written to a file. The tee command is piped with other commands to read input, displays the output on the terminal, and writes the output to a particular file.

To write the input of the below echo command output can be written to the “datafile.txt” by using the below bash script:

#!/bin/sh
FILE=datafile.txt
echo "Welcome to itslinuxfoss" | tee $FILE

Execute the bash script file named “file.sh” with the following command:

$ bash file.sh

The output shows that the datafile.txt file contents are overwritten using the tee command in bash.

We can append the data to a bash file instead of overwriting using the “a” option of the tee command as shown below:

#!/bin/sh
FILE=datafile.txt
echo "Welcome to itslinuxfoss" | tee -a $FILE

Execute the bash script file named “file.sh” with the following command:

$ bash file.sh

The datafile.txt content is appended as shown in the output.

The tee command can be used to write the content to multiple files simultaneously. For instance, to write the data to three files using the bash scripting, run below bash script:

#!/bin/sh
echo "Welcome to itslinuxfoss" | tee datafile.txt datafile2.txt datafile3.txt

Execute the bash script file named “file.sh” with the following command:

$ bash file.sh

The output shows that the same content is written to three different files simultaneously.

These are all the methods for writing o file in bash.

Conclusion

Multiple ways are used to write the content to file in bash. This guide explains the redirect operators, echo, cat, tee, and printf command methods with examples to write the data to a file in bash. Moreover, we can write multiple lines and the same lines in multiple files using the methods mentioned in this guide.