Bash – Use sudo tee with heredoc to append to existing file

bashfileshere-documenttee

From Generate script in bash and save it to location requiring sudo we have this method, which I like:

sudo tee "$OUTFILE" > /dev/null <<'EOF'
foo
bar
EOF

However, I would like to use that approach to append to an existing file $OUTFILE. The above method overwrites existing file $OUTFILE.

Best Answer

You want the -a option to tee which appends rather than overwriting.

Related Question