Linux

Bash Write to File

Bash Write to File

One of the most common tasks when writing Bash scripts or working on the Linux command line is reading and writing files.

This article explains how to write text to a file in Bash, using the redirection operators and tee command.

Writing to a File using Redirection Operators #

In Bash, the redirection of output allows you to capture the output from a command and write it to a file.

The general format for redirecting and writing output to a file is as follows:

output > filename output >> filename 

You need to have write permissions to the file. Otherwise, you will receive a permission denied error.

Here is a simple example showing how the redirect the output of the echo command to a file:

echo "this is a line" > file.txt

To prevent overwriting existing files, enable the “noclobber” option with the set builtin:

set -o noclobberecho "this is a line" > file.txt
bash: file.txt: cannot overwrite existing file 

The >| operator allows you to override the Bash “noclobber” option:

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

The >> operator append the output to the end of the file, rather than overwriting the file:

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

Use the printf command to create a complex output:

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

If you want to write multiple lines to a file, use the Here document (Heredoc) redirection.

For example, you can pass the content to the cat command and write it to a file:

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

To append the lines, change > with >> before the file name:

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

You can write the output of any command to a file:

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

The output of the date command will be written to the file.

Writing to a File using the tee Command #

The tee command reads from the standard input and writes to both standard output and one or more files simultaneously.

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

The tee command's default behavior is to overwrite the specified file, same as the > operator. To append the output to the file, invoke the command with the -a (--append) option:

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

If you don't want the tee to write to the standard output, you can redirect it to /dev/null:

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

To write the text to more than one file, specify the files as arguments to the tee command:

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

Another advantage of the tee command is that you can use it in conjunction with sudo and write to files owned by other users. To append text to a file that you don't have write permissions to, prepend sudo before tee:

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

The echo command output is passed as input to the tee, which elevates the sudo permissions and writes the text to the file.

Conclusion #

In Linux, to write text to a file, use the > and >> redirection operators or the tee command.

If you have any questions or feedback, feel free to leave a comment.

How to reverse Mouse and Touchpads scrolling direction in Windows 10
Mouse and Touchpads not only make computing easy but more efficient and less time-consuming. We cannot imagine a life without these devices, but still...
How to change Mouse pointer and cursor size, color & scheme on Windows 10
The mouse pointer and cursor in Windows 10 are very important aspects of the operating system. This can be said for other operating systems as well, s...
Gry Darmowe i otwarte silniki gier do tworzenia gier na Linuksa
Darmowe i otwarte silniki gier do tworzenia gier na Linuksa
Ten artykuł zawiera listę darmowych i otwartych silników gier, których można używać do tworzenia gier 2D i 3D w systemie Linux. Istnieje wiele takich ...