Your last message doesn't look right.
The "DELIM" will not be written to the file. This method is known as a "here document". It allows for variable expansion as well, and is a common method used to do just what you said you wanted to do.
It allows you to embed documents you want written inside a single script. It was once a common technique used in installation scripts.
$ echo "#!/bin/sh
bash: !/bin/sh: event not found
In an interactive shell, the exclamation point is expanded to a previous bash command. This isn't the case with non-interactive scripts however.
Please refer to the "info bash" manual for the details. Here documents are covered in Section 3.6.5. The Event designator (!) is covered in section 9.3.1.