Generate file contents using cat, here-document and environment variables

A here-document is a I/O redirection that tells the shell to read the data until the delimiter word is found. The <<EOF syntax is very useful when working with multi-line text in bash, eg. when assigning multi-line string to a shell variable, file or a pipe:
dt@ThinkPad-T460s:~$ cat >file.txt <<EOF
echo test && \\
echo $HOSTNAME && \\
echo \$HOSTNAME
EOF
dt@ThinkPad-T460s:~$ cat file.txt 
echo test && \
echo ThinkPad-T460s && \
echo $HOSTNAME
> - outputs to a file. You can use >> also.
<<EOF - read input from the current source until a line containing EOF word is seen. You can name this tag as you want, it's often EOF or STOP.

Quotes or escape prevent parameter expansion, ex:
cat <<'EOF'
or
cat <<\EOF
Hyphen removes leading tabs in the here-document - so the document will loose all formatting with tabs and spaces:
cat <<-EOF
You can use tee instead of cat in case you need redirection outside sudo context:
sudo tee <<EOF /etc/hostname
mynewhostname
EOF
The format of here-documents is:
<<[-]delimiterword
here-document
delimiterword