我想""
在 echo 命令中使用双引号 ( ),如下例所示:
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
我想将此文本放入“/etc/test.conf”中
但是,当我输入这个 bash 时,它返回一个错误:syntax error near unexpected token `('
我该如何修复这个错误?
答案1
有一些 shell 特殊字符将在字符串中的双引号内展开。
考虑到您期望从 shell 进行任何扩展,请使用单引号:
echo '<your_string>' >/etc/test.conf
这将从字面上处理字符串。
答案2
由于您似乎想放置空行,cat
因此可以使用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