如何在bash中逃避感叹号?

ibl*_*lue 9 bash

有时我觉得有必要在我的git提交消息中加入一些表达性.不幸的是,bash似乎并不喜欢这样.

iblue@silence ~/git/wargames $ git commit -m "Frustrating <insert object of frustration here>!"
-bash: !": event not found
Run Code Online (Sandbox Code Playgroud)

使用反斜杠转义有帮助,但这包括提交消息中的反斜杠.

如何正确地逃避bash中的感叹号?

sbl*_*lom 23

当您将其包含在单引号字符串中时,会以字面方式保留感叹号.

例:

git commit -m 'Frustrating <insert object of frustration here>!'
Run Code Online (Sandbox Code Playgroud)

  • Downvote(在所有方面):问题明确地是关于转义感叹号的问题。您的答案确实可以解决上述问题,因此很有帮助,但它不能回答问题本身。我来到这里(使用搜索引擎)时遇到的问题略有不同,我需要真正*转义*感叹号(在双引号内)。您的解决方法对我没有帮助。如果该问题的标题不同(例如“如何解决此感叹号问题”),您的答案将是完全正确的。但是,由于SO是一个问答网站,因此您的答案没有重点。 (2认同)

mug*_*896 8

试试这个

git commit -m "Frustrating <insert object of frustration here>"'!'

如果在字符串的中间那么

"hello"'!'"world"


ceg*_*ult 8

除了使用单引号表示感叹号之外,在大多数 shell 中,您还可以使用反斜杠\对其进行转义。那是: git commit -m "Frustrating <insert object of frustration here>\!"

不过,我个人建议通过在文件中添加或 来禁用 shell 中的 bash 扩展set +Hset +o histexpand.bashrc

如果像我一样,您从不在 shell 中使用 bash 扩展,则禁用它将允许您在任何双引号字符串中使用感叹号 - 不仅在提交期间,而且对于所有bash 命令。


tvm*_*tvm 6

请改用单引号以防止扩展.