How to Write to File in Bash

July 17, 2023

Introduction

Writing data to a file is a common task in Bash scripting. It provides a way to store command output information that would otherwise be temporary. Additionally, writing data to a file facilitates logging and debugging, data backup and archiving, and simplifies data sharing and collaboration.

In this tutorial, you will learn to write to a file in Bash using four different methods.

How to write to a file in Bash - a tutorial.

Prerequisites

  • A system running Linux.
  • An account with root privileges.
  • Access to a terminal (Ctrl + Alt + T).

Write to File via Directional Operators

Directional operators are used in Bash scripting to redirect input and output streams between files and commands. Use them to control where the input comes from and where the output goes.

The directional operators for redirecting output to a file are > and >>. The difference between them is that the > operator overwrites the contents of the specified file if it already exists, while the >> operator appends the output to the end of the file. If the file doesn't exist, it is created.

Write to File Using the > Operator

The following example shows how to use the > operator to write data to a file and overwrite the existing contents. To demonstrate the process, we will use a file named file.txt which has the following contents:

Printing the contents of a file using the cat command.

Use the echo command to print a command to the standard output on the terminal and redirect the output to a file:

echo "Hello, World!" > file.txt

Print the contents of the file again to see if the redirection has worked. Use the cat command:

cat file.txt
Overwriting the contents of a file through Bash redirection.

The file contents have been overwritten with the output from the echo command.

Write to File Using the >> Operator

In this example, we will redirect the ls command output to file.txt from the previous example. The >> operator appends the command output to the existing file contents.

Run the following command:

ls -l >> file.txt

Check if the command output has been appended to the file:

cat file.txt
Redirecting the output of a command to a file in Bash.

The >> operator appends the ls command output to the specified file and keeps its previous contents.

Write to File via tee Command

The tee command reads from the standard input and writes the output to the terminal and any files specified as arguments. That way, tee lets you write to a file while also displaying the output on the screen.

For example:

echo "Welcome to phoenixNAP!" | tee output.txt

The tee command takes the echo command output, displays it in the terminal, and writes it to the specified file. The command creates the file if it doesn't already exist. Check the file contents with cat:

Writing data to a file using the tee command.

Write to File via heredoc

The heredoc feature allows users to write multiple lines of content to a file. It consists of a starting delimiter that marks the beginning, followed by the desired content, and ends with a delimiter that marks the end of the content block.

Heredoc treats its contents as a string literal and maintains the original formatting, which preserves special characters and eliminates the need for escape characters. Bash scripts often use heredocs to simplify the inclusion of large amounts of text or code in a script.

For example, use the following code to write multiple lines of text to the output.txt file using heredoc:

cat << EOF > output.txt
Text line 1.
Text line 2.
EOF

Check if the contents have been written to the file using the cat command:

Writing data to a file using heredocs.

Write to File via printf Command

The Bash printf command produces formatted text output in the terminal. It allows users to control the width, precision, alignment, and other formatting options of the displayed values. The command is beneficial when presenting data in a specific way, with a particular alignment, or with a specific number of decimal places.

Additionally, printf can also be combined with directional operators to write content to a file.

For example:

printf "Line 1\nLine 2\n" > output.txt

The command prints the two lines and a newline character \n between them and then redirects the output to the specified file. Verify the contents are in the file using the cat command:

Writing data to a file using the printf command.

Conclusion

This guide showed you how to write data to a file in Bash using four different methods: directional operators, the tee command, heredoc syntax, and the printf command. Choose the method that best suits your needs and incorporate it into your scripts to manipulate files effectively.

For more Bash tutorials, check out our article on the difference between single and double quotes in Bash, or learn to compare strings in Bash.

Was this article helpful?
YesNo
Bosko Marijan
Having worked as an educator and content writer, combined with his lifelong passion for all things high-tech, Bosko strives to simplify intricate concepts and make them user-friendly. That has led him to technical writing at PhoenixNAP, where he continues his mission of spreading knowledge.
Next you should read
Bash Math Operations Explained
April 14, 2022

You can perform math and arithmetic operations in Bash directly. Although the functionalities are basic, the examples from this tutorial show how to maximize the use of math in Bash scripts.
Read more
Bash String Comparison
September 22, 2022

This tutorial shows how to use the Bash shell in Linux to compare two strings in different ways. See examples for each comparison and create example scripts to learn to compare strings in Bash.
Read more
What Is the .bashrc File in Linux?
January 23, 2023

The .bashrc file holds all the configuration for the Bash shell. Learn how to configure the Bash shell using functions and aliases and create custom colors. Every shell should be unique.
Read more
How to Use Shebang in Bash Scripts
July 13, 2023

The shebang line is the first line of a script that instructs the OS which interpreter to use to execute the script. This tutorial shows how to use the shebang line in Bash.
Read more