How to Write to File in Bash

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


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.


  • 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.

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 1nLine 2n" > 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.


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.

Đăng ký liền tay Nhận Ngay Bài Mới

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !

Add Comment

Click here to post a comment

Đăng ký liền tay
Nhận Ngay Bài Mới

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !