Skip to content

Append to File with Shell

Append output of a command to an existing file with the redirection operator >>

sh
echo "Text to append" >> file.txt

+++

To interpret backslash escaped characters, we can use the -e option:

sh
echo -e "abc\ndef\nghi" >> file.txt

# Content of file.txt:
# abc
# def
# ghi

+++

We can append output of any command, for example:

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

+++

Append with Heredoc. A Here document (Heredoc) is a type of redirection that allows you to pass multiple lines of input to a command. More on heredoc here

sh
cat << EOF >> file.txt
a quick brown fox jumps over the lazy dog
append file with heredoc
EOF