3 Easy Ways to Write to a File in Bash Script

Writing to a file is important to store info for further analysis. Using Bash script one can easily write command output, error and generate reports or logs of a completed task. There are multiple ways to write in a file using Bash script. In this article, I will show you simple but effective methods to write and append data to a file.

Key Takeaways

  • Writing to a new file.
  • Appending data to an existing file.
  • Use of heredoc to write in a file.

Free Downloads

3 Methods of Writing to a File Using Bash Script

Redirection and piping are fundamental concepts in Bash scripting. These offer users a mechanism to write in files. Moreover, the Heredoc technique and printf command provides alternative ways to write in a file. Let’s explore all the techniques to write in a file using Bash script.

You can read the Comparative Analysis of Methods to find one that suits you.

Method 01: Writing in a File by Redirecting the Output in Bash

Using Bash script one can redirect command output to files using the redirection operators. > is used to redirect output, >> is used to append output and 2> is used to redirect error occurs while executing a command. Moreover one can write returning output or error in the same file using 2>&1 redirection.

Steps to Follow >

❶ At first, launch an Ubuntu Terminal.

❷ Write the following command to open a file named output2file.sh in the build-in nano editor:

nano output2file.sh
EXPLANATION
  • nano: Opens a file in the Nano text editor.
  • output2file.sh: Name of the file.
Creating a Bash script file in nano❸ Copy the following scripts and paste them into nano. Press CTRL+O and ENTER to save the file; CTRL+X to exit. Alternatively, copy the following script. Paste the script in a text editor and save it as .sh file.

Script (output2file.sh) >

#!/bin/bash

# Writing output to a file
echo "Files list till:" && $(date) > files.txt
# Appending list of files with files.txt
ls -al >> files.txt

# Redirect errors to error_file.txt file
ls -ahlK > output_file.txt 2> error_file.txt

#  Redirect output and errors to the same file
date > errorout.txt 2>&1

# Attempt to execute an invalid command and redirect errors to errorout.txt
date %Y%M%D >> errorout.txt 2>&1
EXPLANATION

The script begins by echoing the message “Files list till:” followed by the current date using the date command, which is then written to a file named files.txt using > operator.

Next, the script appends the output of the ls -al command to the same files.txt file using >> redirection operator.

Afterward, it runs ls -ahlK to list the files in a human-readable format and sort them by size, with the output saved to output_file.txt. Any errors generated by this command are redirected to error_file.txt using 2> operator.

The script also captures the current date and saves it to the errorout.txt file using 2>&1 operator. An attempt is made to execute an invalid date command with incorrect formatting (date %Y%M%D), which will result in an error. The error message from this invalid command is also appended to errorout.txt.

❹ Use the following two commands to make both file executable:

chmod u+x output2file.sh
EXPLANATION
  • chmod: Changes permissions.
  • u+x: Giving the owner executing permission.
  • output2file.sh: Name of the script.
Changing execution permission of a bash script❺ Run the output2file.sh script by the following command:

./output2file.sh

List of files written in output2file.txtAfter execution, one can see the files.txt file. Here the output of echo “Files list till:” && $(date) is written in the files.txt file.Appending command output in a fileYou can also see the list of files appended to the files.txt because the output ls -al command is appended using the >> operator in the same files.txt file.

Now if you visualize the output_file.txt you may not see anything. This happens due to the error in the command ls -ahlK.Nothing written in the file due to errorLet’s see the error_file.txt as the error of the command is redirected to this file using error redirection (2>).You can see the error_file.txt contains the error that occurs while executing ls -ahlK command. It shows an invalid option error regarding the use of -K with the ls command.Writing error and output in the same fileThe errorout.txt file shows that the output of date command is written in the file along with the error of the next date %Y%M%D command.

Method 02: Writing in a File Using “tee” Command

One can easily write to a file using pipe and tee command in Bash. Let’s say I want to write the last ten executed commands in a file.

You can follow the Steps of Method 1 to learn about creating and saving shell scripts.

Script (tee.sh) >

#!/bin/bash

history | tail -n 10 | tee shell_history.txt
EXPLANATION

The Bash script is designed to retrieve the last 10 commands entered in the current session’s command history and save them to a text file named shell_history.txt. It uses the history command to access the list of previously executed commands. The tail -n 10 command filters the output, showing only the last 10 entries from the command history.

Finally, the tee command takes the filtered output and simultaneously displays it on the terminal and writes it to the shell_history.txt file in the current directory.

Run the tee.sh script by the following command:

. ./tee.sh

Tee command to write history in a fileThe script shows the ten most recently executed commands, which are also recorded in the shell_history.txt file.

NOTE: Make sure you execute the script using the . ./tee.sh command. If you execute the script using ./tee.sh command then it will execute in a different shell session with no previous shell history. Alternatively, you can use the command below.
source ./tee.sh

Let’s see the shell_history.txt file using the following command which should contain the same list as well:

cat shell_history.txt

List of history written in the fileYou can see the same list of last 10 commands is written in the shell_history.txt file.

Method 03: Use of “Heredoc” to Write in a File

Heredoc (short form of here document) is a block of text that starts with a marker and continues until it reaches the marker again. It is a convenient way to include multiple lines of text within a command or script. Look at the Heredoc below to have a better understanding.

<< EOF

This is the first line.
Second line.
Heredoc ends here.
EOF

In this method, I am going to use a Heredoc like above to write in a file. I will use <<EOF (End of File) as the marker to start and end the Heredoc. You can use any marker such as EOM (End of Message) or anything you want to quote the Heredoc block.

You can follow the Steps of Method 1 to learn about creating and saving shell scripts.

Script (heredoc.sh) >

#!/bin/bash

cat << EOF > file.txt
You are logged in as $(whoami)
The current working directory is: $(pwd)
EOF
EXPLANATION

The provided script utilizes a here document (indicated by << EOF) to efficiently write multiple lines of text into a file named file.txt. The first line of the document displays the username of the logged-in user using the whoami command. The second line reveals the current working directory employing the pwd command. The script concludes by closing the here document with EOF, thus effectively populating the text and command output into the “file.txt” with the pertinent data.

Run the heredoc.sh script by the following command:

./heredoc.sh

Use of Heredoc to write in a fileHere the program writes the text or command output within the Heredoc block in file.txt. If we see the file.txt file, we can see that the desired text with command output is written in the file.

Comparative Analysis of Methods

Here is a comparative analysis between the above-discussed methods. Take a look at the analysis to determine which method suits your needs.

Method Advantage Disadvantage
Redirection Operators
  • Can redirect text and command output very efficiently.
  • May accidentally overwrite unintended contents of a file.
Tee Command
  • One can see the text and write it to a file simultaneously.
  • Doesn’t offer advanced formatting like printf.
Heredoc
  • Easy to add multiple lines.
  • Tough to add complex formatting in a heredoc.

When it comes to writing in a file using Bash script, the preferable method depends on the nature of the task. I will definitely go with redirection operators if I have to save command output in a file. On the other hand, if the task is like writing a long text into a file I may utilize the Heredoc technique. So, my suggestion is to choose the method wisely after understanding the nature of your task.

How to Use “print” Command to Write in a File in Bash Script

It is wise to use printf command if you are writing formatted output to a file. You can use any method to write into a file while using printf command. Here I am using the redirection operator to write formatted text into a file.

You can follow the Steps of Method 1 to learn about creating and saving shell scripts.

Script (formattext.sh) >

#!/bin/bash

filename="format.txt"
# Writing formatted content to the file
printf "Hello, this is a formatted message.\n\
This is the second line.\n\
The value of the variable is: %s\n" "42" > "$filename"
echo "Content has been written to $filename"
EXPLANATION

In this script, the variable filename is assigned the value format.txt, specifying the name of the output file. The printf command is then utilized to compose a formatted message that includes multiple lines of text. This message is structured using line breaks and a placeholder for the value “42.”

The output of the printf command is redirected using the > operator to the designated output file format.txt. Subsequently, an echo command  is used to provide a confirmation message.

Run the heredoc.sh script by the following command:

./formattext.sh

Writing formatted text to file using printf commandAs you can see the text written in the format.txt file is formatted as expected. Here \n indicates line break or continuation of a new line. And %s is used for placing values such as 42.

How to Prevent Overwriting in a File

The set -o noclobber command is used to temporarily prevent overwriting in an existing file. If this option is enabled, you won’t be able to use the > operator to write to an existing file. However, you can still use the >> operator to append content to the file.

You can follow the Steps of Method 1 to learn about creating and saving shell scripts.

Script (prevent.sh) >

#!/bin/bash

echo "Starting script at $(date)" > script_log.txt
set -o noclobber
echo "Directory of the current script:" $(pwd) >> script_log.txt
echo "Total files in this directory:" $(ls -l | wc -l) >> script_log.txt

sleep 2

echo "Script completed at $(date)" > script_log.txt
set +o noclobber
EXPLANATION

This Bash script begins by logging the current date into a file called script_log.txt. The set -o noclobber command prevents accidental file overwrites. The script proceeds by appending the current working directory using the pwd command and the count of files in the directory using ls -l | wc -l.

Following a brief two-second pause by sleep 2, the script wants to record the completion time of the script. But it can not overwrite script_log.txt file due to noclobber option being enabled. Finally, the set +o noclobber command deactivates the noclobber option.

Run the tee.sh script by the following command:

./prevent.sh

Preventing file from being overwrittenOnce you execute the program, it saves the starting time and output of the command pwd and ls -l | wc -l in the script_log.txt file. However, it can’t overwrite the content of the file and add completion time. Rather it shows an issue says “cannot overwrite existing file” as noclobber is enabled.

In cases where an existing file is protected by the set command to prevent accidental overwriting. You can still deliberately overwrite the file using the >| operator.

Conclusion

In conclusion, redirection operators (>, >>) offer an easy way to write into files. Moreover one can use Heredoc to efficiently write from the terminal. On the other hand, one can add an extra layer of protection in terms of overwriting a file using the noclobber option of the set command.

I believe after reading this article you can easily write to a file in Bash whether it is for generating logs, creating reports, or storing processed data.

People Also Ask

Bash script to write in a file without echo command?
You can either use the cat command along with a Heredoc to write into a file without using echo command.
How to append text to an existing file in new line?
While using the echo command to append text in an existing file you can enable -e option to write the text in a new line. Alternatively, if you use the printf command to write to a file you can use “\n” to add a new line.
How to write into a file from Linux terminal?
To write into a file directly from the terminal, you can use the echo command and push the text to the file using the redirection operator.

Related Articles


<< Go Back to Bash Script Examples | Bash Scripting Basics | Bash Scripting Tutorial

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
Md Zahidul Islam Laku

Hey, I'm Zahidul Islam Laku currently working as a Linux Content Developer Executive at SOFTEKO. I completed my graduation from Bangladesh University of Engineering and Technology (BUET). I write articles on a variety of tech topics including Linux. Learning and writing on Linux is nothing but fun as it gives me more power on my machine. What can be more efficient than interacting with the Operating System without Graphical User Interface! Read Full Bio

Leave a Comment