This question already has an answer here:

I want to echo some unique string to a file. Sample code like bellow:

echo "    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined"
echo "    LogFormat "%h %l %u %t \"%r\" %>s %b" common"
} >> file.conf

When i run it, file.conf become seperate in to 2 file. How i can echo it with a correct way?

Thank You

marked as duplicate by ilkkachu, Kusalananda, Satō Katsura, Tim Kennedy, thrig Mar 20 '17 at 16:01

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    You have an unquoted double-quote and an unquoted > redirection in the string you're trying to echo, so the output goes to file s. Either escape the quotes within the quotes, or put the whole string in single-quotes (') so you can include double-quotes (and backslashes) without quoting. echo ' LogFormat "%h %l %u %t \"%r\" %>s %b" common' – ilkkachu Mar 20 '17 at 6:15
echo '  LogFormat "%h %l %u %t \"%r\" %s> %b \"%{Referer}i\" \"%{User-Agent}i\" combined"'
echo '  LogFormat "%h %l %u %t \"%r\" %>s %b common"'
} >> file.conf

Since you are not doing any double quote interpolation, then it makes it easy to move your echo string inside of single quotes '...' to simplify quoting.

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