2

I want to use double quotes ("") inside of echo command like this example below:

echo "  
 ^%(log_prefix)s SecurityEvent=(FailedACL|InvalidAccountID|ChallengeResponseFailed|InvalidPassword)",EventTV="[\d-]+",Severity="[\w]+",Service="[\w]+",EventVersion="\d+",AccountID="\d+",SessionID="0x[\da-f]+",LocalAddress="IPV[46]/(UD|TC)P/[\da-fA-F:.]+/\d+",RemoteAddress="IPV[46]/(UD|TC)P/<HOST>/\d+"(,Challenge="\w+",ReceivedChallenge="\w+")?(,ReceivedHash="[\da-f]+")?$

 " > /etc/test.conf

And I want to put this text inside of '/etc/test.conf'

But, when I type this bash, it returns me an error: syntax error near unexpected token `('

How can I fix this error?

  • Since you're not using any variables, use echo '....' - ie single ' and not " – Stephen Harris Aug 16 '16 at 13:56
  • Spotted this: >/\d+"(,Cha – EKons Aug 16 '16 at 14:06
  • Can you format your code better, please? (I'll suggest an edit) – EKons Aug 16 '16 at 14:09
4

There are some shell special characters that will be expanded within double quotes present in your string.

Use single quotes instead given you are expecting any expansion from shell:

echo '<your_string>' >/etc/test.conf

This will treat the string literally.

1

Since you seem to want to put empty lines, you can use cat instead of echo:

cat << 'EOT' > /etc/test.conf

 ^%(log_prefix)s SecurityEvent=(FailedACL|InvalidAccountID|ChallengeResponseFailed|InvalidPassword)",EventTV="[\d-]+",Severity="[\w]+",Service="[\w]+",EventVersion="\d+",AccountID="\d+",SessionID="0x[\da-f]+",LocalAddress="IPV[46]/(UD|TC)P/[\da-fA-F:.]+/\d+",RemoteAddress="IPV[46]/(UD|TC)P/<HOST>/\d+"(,Challenge="\w+",ReceivedChallenge="\w+")?(,ReceivedHash="[\da-f]+")?$

EOT
  • 1
    You'd still need to watch out for backslashes, $ and ` there. Use cat << 'EOT' if you don't want any expansion to take place there. – Stéphane Chazelas Aug 16 '16 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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