有时我觉得有必要在我的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)
试试这个
git commit -m "Frustrating <insert object of frustration here>"'!'
如果在字符串的中间那么
"hello"'!'"world"
除了使用单引号表示感叹号之外,在大多数 shell 中,您还可以使用反斜杠\对其进行转义。那是:
git commit -m "Frustrating <insert object of frustration here>\!"
不过,我个人建议通过在文件中添加或 来禁用 shell 中的 bash 扩展。set +Hset +o histexpand.bashrc
如果像我一样,您从不在 shell 中使用 bash 扩展,则禁用它将允许您在任何双引号字符串中使用感叹号 - 不仅在提交期间,而且对于所有bash 命令。