0

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

  • 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 06:15

1 Answers1

1
{
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.