简体   繁体   中英

How do I escape an exclamation mark in bash?

Sometimes I feel the urge to put some more expressiveness in my git commit messages. Unfortunately bash does not seem to like this.

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?

Exclamation mark is preserved literally when you include it in a single-quoted string.

Example:

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

Have a try this one

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

If in the middle of string then

"hello"'!'"world"

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

In addition to using single quotes for exclamations, in most shells you can also use a backslash \\ to escape it. That is: git commit -m "Frustrating <insert object of frustration here>\\!"

However, I personally recommend disabling bash expansion in you shell by adding set +H or set +o histexpand to your .bashrc file.

If, like me, you never use bash expansion in your shell, disabling it will allow you to use exclamation points in any double-quote string - not only during your commits but for all bash commands.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM