Using "sed" to append to end of a file
The usual replacement for shell >
with higher/different privileges is:
echo "replace file content with this line" | sudo tee protectedFile >/dev/null
And if you want to append, use -a
:
echo "append this line to file" | sudo tee -a protectedFile >/dev/null
Whithout echo
:
string=$'"<VirtualHost *:443> \
... \
</VirtualHost>"'
sudo sed -i '$a\'"${string}"'' file
The -i
option tells sed
to process a files in-place (optionally adding a suffix to the original version).
The sed script:
$
match last linea\
append text after matching line
It won't work on an empty file though (since there won't be any lines of input to match the last line).