How to Open and Write Data to Text File Using Bash?

Bash is a powerful scripting programming-based environment examined for automating tasks in Linux. Writing data in the text file is a very common task to save the output of any command for keeping the record of a particular command’s execution. Bash scripts come with various methods to write data in the text file such as using the redirection operator, tee command, and while loop. 

This post will enlighten the methods to write data to the text file in Bash script.

  • Method 1: Using Redirection Operator
  • Method 2: Using while Loop
  • Method 3: Using tee Command

Method 1: Using the Redirection Operator

The redirection operator is a commonly used feature in Bash that enables the user to redirect the command’s output. The user can use the redirection operator to redirect its output to the text file either an existing or a new file. Follow the below syntax to apply this method in the Bash script:

Syntax

$ [command]  > [File Name]    #For Creating New File
$ [command]  >> [File Name]   #Appends Data in Existing File

The syntax is defined as

  • Enter the [Command] along with the redirection operator “>” to create the new file.
  • Enter the [Command] along with the redirection operator “>>” to append data to an already existing file.

Example 

In the subsequent script will write the data into the new file “file.txt” and existing file “E-File.txt”:

#!/bin/bash

#Creates the New File
echo "Hi, This is Henry From itslinuxfoss" > file.txt

#Appends Data in ALready Existing File
ls -l file.txt >> E-File.txt

The script is defined as

  • Redirecting the “echo” command’s output and writing data into the new file “file.txt” through “>” operator 
  • Redirecting the “ls -l file.txt” command’s output into the existing file “E-File.txt” through the “>>” operator.

Save the script and exit from the file.

Run the script using the bash command:

$ bash script.sh

The script is executed and the data is written in both files.

Let’s verify the content of the both “file.txt” and “E-File.txt” using the cat command:

$ cat file.txt
$ cat E-File.txt

The given “echo” statement is written in the “file.txt” and as well as output of the “ls -l file.txt” is appended in the “E-File.txt” file.

Method 2: Using the while Loop

The while loop is considered for repeating the tasks in the Bash script. Interestingly, it provides the feature to take input from the user and write it into the specified file. Here’s the syntax of using the while loop to write data into the file.

Syntax 

#!/bin/bash

[File_variable]=[File_Name]
echo statement
while read line
do
    echo $line > [File_variable]
done

The syntax is defined as

  • Define the “[File_variable]” of your choice and store the “[File_Name]” in it.
  • The echo statement prints the message to the user.
  • The “while” loop with the “read line” property to read every entered line.
  • Then the “do” section that is redirecting each line into the specified file name stored in the “[File_variable]”.

Example

The following script will take the input from the user and write it into the “new-file.txt”:

#!/bin/bash

file=new-file.txt
echo "Enter Content to Write it in the $file File."
while read line
do
    echo $line > $file
done

The script is defined as 

  • The “file” variable that stores the file name “new-file.txt
  • The “echo” is printing the message to the user to enter data by specifying the file name.
  • The “while” loop is taking the input from the user and reads each line entered by the user.
  • Then, the “do” section is writing the data into the “new-file.txt” using the redirection “>”.

Save the above script and exit.

Run the script and enter data to write it into the file “new-file.txt”:

$ bash script.sh

The data is entered, press the “Ctrl+D” once you finish your content.

Let’s verify the content of the “new-file.txt” in the terminal:

$ cat new-file.txt

The entered data is available in the “new-file.txt” as shown above.

Method 3: Using tee Command

Another way to enter data into the text file is by using the tee command. It’s working just like the redirection operator that creates and writes data into the new file or appends data to the existing file. Below is the syntax for using the tee command in Bash.

Syntax 

$ [command]  | tee [File Name]      #Creates the New File
$ [command]  | tee -a [File Name]   #Appends in the Existing File

The syntax is defined as

  • Enter any [Command] along with the “tee” command separated by the pipe (|) and specify the [File_Name]. 
  • Enter any [Command] along with the “tee” command separated by the pipe (|) and specify the “a” flag with [File_Name] to append data

Example 

In this example echo

#!/bin/bash

#Creates the New File
echo "Hi, This is Henry From itslinuxfoss" | tee tee.txt

#Appends Data in ALready Existing File
uname -a | tee -a E-tee.txt

The script is defined as 

  • Sending the “echo” command’s output to the tee command and writing data into the new file “tee.txt” through pipe(|).
  • Sending the “uname -a” command’s output (prints information about the system) to the tee and the “a” flag appending data into the existing file “E-tee.txt” through pipe(|).

Save the script and exit.

The one thing that can be noticed here is that running the above script, the tee command also displays the output on the screen. Run the above script and test it:

$ bash script.sh

The output is shown on the screen and written in the specified file.

Let’s verify the content of both files “tee.txt” and “E-tee.txt”:

$ cat tee.txt
$ cat E-tee.txt

The given “echo” statement is written in the “tee.txt” and as well output of the “uname -a” is appended in the “E-tee.txt” file.

Conclusion

In the Bash script, the data is written into the text file using the redirection operator (>, >>), tee command, or while loop. For the while loop, the user can use the “read line” property to take the input from the user for writing data into the specified file.

This write-up has briefly illuminated the most useful methods to write data into the text file using the Bash script.