If I have some nested and indented statements that echo a multi-line string into a file, the indents from the original shell (in my case bash) will be carried over into the target file.
To avoid this, I remove indents from the echo'ed output, but this loses that indented code formatting in my source, for example,
#! /bin/bash
function configure_default_vhost() {
case "$2" in
[--production])
echo "<VirtualHost *:80>
# These lines are "breaking" out of the preffered indenting
redirect 404 /
ErrorDocument 404
</VirtualHost>
" > /etc/apache/sites-availabe/000-default.conf
esac
}
I'm aiming to get something as close as possible to this:
#! /bin/bash
function configure_default_vhost() {
case "$2" in
[--production])
echo "<VirtualHost *:80>
# These lines are aligned with the preffered indenting
redirect 404 /
ErrorDocument 404
</VirtualHost>
" > /etc/apache/sites-availabe/000-default.conf
esac
}
(Note: this question has been listed as a possible duplicate of a HEREDOC related question. I'm not sure where the correct place to respond to this is so I'll put here for now (someone please let know if otherwise). My question is about indenting code blocks, and heredoc is only one of the many ways to acheive this and actually HEREDOC wasn't the solution I went with.)
cat <<-EOF
). See http://unix.stackexchange.com/questions/76481/cant-indent-heredoc-to-match-nestings-indent for details. – Mikel Apr 24 '15 at 14:09