How can I write a here document to a file in Bash script?
Instead of using cat and I/O redirection it might be useful to use tee instead:
cat
tee
tee newfile <
It's more concise, plus unlike the redirect operator it can be combined with sudo if you need to write to files with root permissions.
sudo