I'm working with a bash script trying to stop it from attempting to replace variables inside my heredoc. How do set a heredoc to either A) escape the variable names instead of parsing them or B) return the entire string untouched?

cat > /etc/nginx/sites-available/default_php <<END
server {
    listen 80 default;
    server_name _;
    root /var/www/$host; <--- $host is a problem child
}
END

As is, when I it finishes injecting it into a file I'm left with this:

server {
    listen 80 default;
    server_name _;
    root /var/www/;
}
up vote 30 down vote accepted

From the bash(1) man page:

If any characters in word are quoted, the delimiter is the result of quote removal on word, and the lines in the here-document are not expanded.

cat > /etc/nginx/sites-available/default_php <<"END"

Just with a backslash:

cat > /tmp/boeboe <<END
server {
    listen 80 default;
    server_name _;
    root /var/www/\$host';
}
END
  • Do you know how to disable injection of variable values without escaping each one? – Xeoncross Jun 16 '12 at 15:59
  • 2
    What Ignacio said, but I didn't know that :) – Halfgaar Jun 17 '12 at 6:53

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.