Oct 20, 2023 4 min read

Bash: Write to File

Learn bash: write to file with our step-by-step tutorial. Bash is a widely used command-line shell and scripting language in Unix-like OS.

Bash: Write to File
Table of Contents

Introduction

Before we start discussing bash: write to file, let's first understand-What is Bash?

Bash, short for "Bourne Again Shell," is a widely used command-line shell and scripting language in Unix-like operating systems. It provides a text-based interface for users to execute commands, automate tasks, and interact with the operating system and file system.

In Bash, you can write content to files using various methods and redirection operators. Being able to write to files is essential for creating and updating configuration files, logs, and other text-based data.

In this tutorial, we will explain how to write text to a file in Bash, using the redirection operators and tee command. We will also address a few FAQs on bash: write to file.

Writing to a File using Redirection Operators

With Bash's output redirection feature, you may gather a command's output and save it to a file.

The following is the usual format for output redirection and writing to a file:

output > filename 
output >> filename 
  • The output is written to a specified file via the > redirection operator. The file is shortened to zero length if it is present. If not, the file is created. Use this operator with extreme caution as you can accidentally overwrite a crucial file.
  • The output is appended to a specified file via the >> redirection operator. If the file doesn't already exist, one is created.

You must have the permission to write to the file, otherwise you will get a permission denied error.

Here is a straightforward example demonstrating how to route the echo command's output to a file:

echo "this is a line" > file.txt

The "noclobber" option with set should be enabled to avoid overwriting existing files:

set -o noclobber
echo "this is a line" > file.txt
Output

bash: file.txt: cannot overwrite existing file

You may override the Bash "noclobber" option by using the >| operator:

set -o noclobber
echo "this is a line" >| file.txt

Instead of overwriting the file, the >> operator appends the output at the end of the file:

echo "this is a line" >> file.txt

In order to produce a complicated result, use the printf command:

printf "Hello, I'm %s.\n" $USER > file.txt

Use the Here document (Heredoc) redirection if you wish to write numerous lines to a file.

As an example, you may use the cat command to write the content to a file:

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

Change > with >> before the file name to attach the lines:

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

Any command output may be written to a file:

date +"Year: %Y, Month: %m, Day: %d" > file.txt

The date command's output will be saved to the file.

Writing to a File using the tee Command

The tee command concurrently reads from standard input and writes to one or more files, as well as standard output.

echo "this is a line" | tee file.txt

Similar to the > operator, the tee command overwrites the chosen file by default. Use the -a (--append) option when running the command to attach the output to the file:

echo "this is a line" | tee -a file.txt

You may ask the tee to write to /dev/null if you don't want it to write to standard output:

echo "this is a line" | tee file.txt >/dev/null

If you want to write the text to several files, provide the files as arguments to the tee:

echo "this is a line" | tee file_1.txt file_2.txt file_3.txt

The ability to use the tee command in combination with Sudo and writing to files held by other users is another benefit. Prepend Sudo before tee to add text to a file that you do not have access to write to:

echo "this is a line" | sudo tee file.txt

The tee program raises the sudo permissions and writes the text to the file using the output from the echo command as input.

FAQs on Bash:Write to File

How can I write text to a file using Bash? 

You can write text to a file using the echo command and redirecting the output to a file. For example:

echo "Hello, World!" > file.txt

What if I want to append text to an existing file without overwriting its contents? 

To append text to an existing file, use the >> redirection operator instead of >.

How can I write the output of a command to a file? 

You can write the output of a command to a file using the command > file.txt

How do I create a new file and write to it using a single command? 

You can use the > redirection operator with an empty string to create a new file and write to it simultaneously. 

Can I write multiple lines of text to a file in Bash? 

Yes, you can use a multi-line echo statement or input redirection to write multiple lines of text.

How can I write the error output of a command to a file? 

Use the 2> operator to redirect the error output (stderr) to a file.

Is there a way to write both standard output and error output to a file?

Yes, you can use the &> operator to redirect both standard output and error output to a file.

Conclusion

Use the > and >> redirection operators or the tee command in Linux to write text to a file.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Blog - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.