FUNDAMENTALS A Complete Guide for Beginners
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.
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.
❶ 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
- nano: Opens a file in the Nano text editor.
- output2file.sh: Name of the 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
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
- chmod: Changes permissions.
- u+x: Giving the owner executing permission.
- output2file.sh: Name of the script.
./output2file.sh
After execution, one can see the files.txt file. Here the output of echo “Files list till:” && $(date) is written in the files.txt file.You 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.Let’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.The 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.
Script (tee.sh) >
#!/bin/bash
history | tail -n 10 | tee shell_history.txt
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
The script shows the ten most recently executed commands, which are also recorded in the shell_history.txt file.
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
You 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.
Script (heredoc.sh) >
#!/bin/bash
cat << EOF > file.txt
You are logged in as $(whoami)
The current working directory is: $(pwd)
EOF
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
Here 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 |
|
|
Tee Command |
|
|
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.
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"
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
As 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.
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
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
Once 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
Related Articles
- How to Get Date in Bash [2 Methods with Examples]
- How to Print Time in Bash [2 Quick Methods]
- How to List Users in Bash [2 Easy Ways]
- How to Get Current Time in Bash [4 Practical Cases]
- How to Use Date Format in Bash [5 Examples]
- How to Get Timestamp in Bash [2 Practical Cases]
- How to Copy and Paste in Bash [2 Methods & Cases]
- How to Read Password in Bash [3 Practical Cases]
- How to Send Email in Bash [2 Easy Methods]
- Bash Script to Send Email with Attachment [Step-by-Step Guide]
- How to Get IP Address in Bash [3 Methods]
- How to Find and Replace String in Bash [5 Methods]
- How to Get Script Name Using Bash Script? [3 Easy Ways]
- How to Call Another Script in Bash [2 Methods]
- How to Generate UUID in Bash [3 Simple Methods]
- How to Write the Output to a File in Bash Script [5 Practical Cases]
- How to Create a List in Bash Scripts? [2 Easy Methods]
- How to Clear History in Bash [2 Practical Cases]
- How to Clear Screen Using Bash Script? [2 Effective Methods]
- How to Check Ubuntu Version Using Bash Scripts? [2 Methods]
<< Go Back to Bash Script Examples | Bash Scripting Basics | Bash Scripting Tutorial