Bash: Append to File

Updated on

3 min read

Bash: Append to File

When working with Bash, there might be times when you need to append text to a file. Fortunately, there are multiple ways to accomplish this task. This article explains some of them.

To append text to a file, you need to have write permissions to it. Otherwise, you will receive a permission denied error.

Append to a File using the Redirection Operator (>>)

Redirection allows you to capture the output from a command and send it as input to another command or file. The >> redirection operator appends the output to a given file.

There are a number of commands that you can use to print text to the standard output and redirect it to the file. The two most commonly used commands for this purpose are echo and printf.

To append text to a file, run the command that prints the text and specify the name of the file after the redirection operator:

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

When used with the -e option, the echo command interprets the backslash-escaped characters such as newline \n:

echo -e "this is a new line \nthis is another new line" >> file.txt

To produce more complex output, you can use the printf command, which allows you to specify the formatting of the output:

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

Another way to append text to a file is to use the Here document (Heredoc). It is a type of redirection that allows you to pass multiple lines of input to a command.

For instance, you can pass the content to the cat command and append it to a file:

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

You can append the output of any command to a file. Here is an example with the date command:

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

When appending to a file using a redirection, be careful not to use the > operator to overwrite an important existing file.

Append to a File using the tee Command

tee is a Linux command-line utility that reads from the standard input and writes to both standard output and one or more files at the same time.

By default, the tee command overwrites the specified file. To append the output to the file use tee with the -a (--append) option:

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

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

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

The advantage of using the tee command over the >> operator is that tee allows you to simultaneously append text to multiple files and write to files owned by other users in conjunction with sudo.

To append text to a file that you don’t have write permissions to, prepend sudo before tee as shown below:

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

tee receives the output of the echo command, elevates the sudo permissions, and writes to the file.

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

echo "this is a new line"  | tee -a file1.txt file2.txt file3.txt

Conclusion

If you’re working with Linux and need to add some text to an existing file, you have a couple of options. One way is to use the >> redirection operator, which will append the text to the end of the file without overwriting any existing content. Another option is to use the tee command, which not only appends the text to the file, but also displays it on the screen as it’s being added.

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