Use sudo tee with heredoc to append to existing file
You want the -a
option to tee
which appends rather than overwriting.
If you prefer to use cat
, then go this route, first passing $OUTFILE
as your output's receptacle:
$ OUTFILE=/path/to/restricted_write_access/file
$ sudo out=$OUTFILE sh -c 'cat << EOF >> $out
foo
bar
EOF'