45

Sometimes I feel the urge to put some more expressiveness in my Git commit messages. Unfortunately, Bash does not seem to like this:

cd ~/git/wargames git commit -m "Frustrating <insert object of frustration here>!" 

Output:

-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?

3

4 Answers 4

54

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

Example:

git commit -m 'Frustrating <insert object of frustration here>!' 
Sign up to request clarification or add additional context in comments.

3 Comments

Downvote (with all respect): The Question is explicitly about escaping an exclamation mark. Your answer does solve the problem described above, is helpful as such, but it doesn't answer the question itself. I came here (using a search engine) with a slightly different problem and I need to really escape the exclamation mark (inside double quotes). Your workaround doesn't help me. If this question had a different title (e.g. "how to solve this exclamation mark problem") your answer would be perfectly fine. But as SO being a question & answer site, your answer misses the point.
I came with a question "How to keep bash from putting its fingers into my arguments" and this answer helped me.
Indeed, mug896 answer is better. Imagine the case when you already need the single quotes, for instance: psql -c "SELECT * FROM player WHERE name !~ '^admin'".
35

Have a try with this one:

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

If in the middle of string, then:

"hello"'!'"world" 

Comments

12

Use single quotes instead to prevent expansion.

1 Comment

If it is part of a string you need to separate it by doing " ' ! ' " without the spaces I added
10

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 BVash 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.

2 Comments

I tried escaping ! in a double quoted string with ` while using grep` recently in Bash. It didn't work for me, and I've confirmed it failed for the same reason that it failed for the OP.
Re "...in most shells you can also use a backslash \ to escape it": I don't think that works in Bash (in a default configuration). Try for example 'printf "Hello, World\!\n"' (it will output the literal '\!': 'Hello, World\!'). Whereas 'printf "Hello, World"'!'"\n"' will output 'Hello, World\!'. It was tried with Bash 5.0.17. Just 'printf "Hello, World!\n"' will result in 'bash: !\n: event not found'.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.