# Append to File with Shell

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

echo "Text to append" >> file.txt

+++

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

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

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

+++

We can append output of any command, for example:

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

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