如何在 bash 中转义感叹号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11025114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I escape an exclamation mark in bash?
提问by iblue
Sometimes I feel the urge to put some more expressiveness in my git commit messages. Unfortunately bash does not seem to like this.
有时我会想在我的 git commit 消息中加入更多的表现力。不幸的是,bash 似乎不喜欢这样。
iblue@silence ~/git/wargames $ git commit -m "Frustrating <insert object of frustration here>!"
-bash: !": event not found
Escaping with a backslash helps, but this includes the backslash in the commit message.
使用反斜杠转义会有所帮助,但这包括提交消息中的反斜杠。
How do I escape the exclamation mark in bash correctly?
如何正确转义 bash 中的感叹号?
回答by sblom
Exclamation mark is preserved literally when you include it in a single-quoted string.
当您将感叹号包含在单引号字符串中时,它会按字面保留。
Example:
例子:
git commit -m 'Frustrating <insert object of frustration here>!'
回答by mug896
Have a try this one
试试这个
git commit -m "Frustrating <insert object of frustration here>"'!'
git commit -m "Frustrating <insert object of frustration here>"'!'
If in the middle of string then
如果在字符串的中间然后
"hello"'!'"world"
"hello"'!'"world"
回答by tvm
Use single quotes instead to prevent expansion.
请改用单引号以防止扩展。

