In bash, how do I escape an exclamation mark?

194

39

I want to do something like bzr commit -m "It works!". I can sort of escape the exclamation mark by doing bzr commit -m "It works\!". However, then my commit message includes the backslash. How do I escape the exclamation mark, while still ignoring the backslash?

Matthew

Posted 2010-04-22T18:53:43.307

Reputation: 11 686

1As I noted before the command you put does actually work on it's own :) bzr commit -m "It works!" – h4unt3r – 2014-08-18T03:26:53.407

An even quirkier case with nested quotes: http://stackoverflow.com/questions/22125658/how-to-escape-history-expansion-exclamation-mark-inside-a-double-quoted-comm

– Ciro Santilli 新疆改造中心法轮功六四事件 – 2015-09-02T18:08:33.370

1

Though the accepted answer is a good workaround for your commit problems, I feel it's not an answer to the actual issue: Bash history expansion. Please consider accepting Dennis' answer?

– Arjan – 2015-11-29T10:19:08.510

@h4unt3r Doesn't appear to work for me? echo "It works!" -bash: !": event not found – rogerdpack – 2020-02-10T18:24:13.273

1Doing bzr commit -m "It works"! works, too. – kba – 2013-05-18T13:59:17.493

Answers

159

Since you do not depend on bash to expand variables in your commit message you could use single quotes instead. Strings in single quotes are not expanded by bash.

bzr commit -m 'This does work!' 

Benjamin Bannier

Posted 2010-04-22T18:53:43.307

Reputation: 13 999

I agree with both Benjamin and Jann but gave a downvote. The title of the question is indexed by search engines and that's how I came here and probably most users come here. That's why I believe it's important that the actual question reflect's the problem behind it. In other forums people are advised (almost forced) to ask precisely what they look for. Answering poorly expressed questions supports sloppiness and that's the exact reason for my downvote here. As a compromise / solution my suggestion is to help adapt the heading so it reflects the actual problem. – ChristophK – 2018-01-06T10:49:37.780

24Since the question was "How do I escape an exclamation mark?" and not "How do i not expand an exclamation mark?" I do not think this is valid. There are some times (like when passing apostrophes and exclamation marks in the same command-line) that this does not work. The answer below works much better to do what many people need. – Jann – 2011-05-24T17:55:05.253

7@Jann: You are 100% right on this, but I think the issue is here is the one with so many questions on superuser: Do we answer the question to the point, or do we help people solve their specific problem. I think both ways can be useful, and this was looking for help on a specific issue. – Benjamin Bannier – 2011-05-24T18:17:49.087

touché! This was a specific issue. I just tend to want an answer to my questions to be able to be used in many situations. pS: The reason I even mentioned this was I was needing to pass both an apostrophe and an exclamation mark to a perl program and I needed the below solution. :) – Jann – 2011-05-24T18:37:04.453

155

Here is another method if you want double quotes as well as the exclamation:

echo "It's broken"'!'

This works even if the ! is not at the end of the line.

For instance:

echo "hello there"'!'" and goodbye"

Bonus: A similar technique can be used to escape any text in Sh or Bash (with the help of sed): see the first option in this answer. Further, if you have bash-completion installed, you likely have the quote() function available already.

jwd

Posted 2010-04-22T18:53:43.307

Reputation: 2 652

Actually if the exclamation mark is at the end of the string you're in the clear echo "Happy birthday!" will work as expected otherwise you can escape it with a backslash, but the backslash will be printed as well XD Bash is not for the faint of heart :) – h4unt3r – 2014-08-18T03:24:10.157

@h4unt3r: Strange, that is not my experience. For me, echo "Happy Birthday!" outputs 2 lines. The first is echo "Happy birthday" (no exclamation), and the second is "Happy birthday" (again, no exclamation). Do you have history expansion turned on when you do this test? – jwd – 2014-08-18T21:05:56.910

@jwd what version of bash are you running? I always have hist expansion on. Did you accidentally use two !! for your test? – h4unt3r – 2014-08-19T00:15:52.753

@h4unt3r: version 4.1.2(1)-release (x86_64-redhat-linux-gnu), definitely only one ! being used. I have histexpand present in $SHELLOPTS. However, I did just try 4.3.18(2)-release on another machine, and it behaves as you described. I guess it was a bug that got fixed. – jwd – 2014-08-19T20:28:17.473

Ugly, but works...darn bash... – rogerdpack – 2020-02-10T18:25:56.227

4Nice tip, JWD. I didn’t realize bash strings could be concatenated simply by omitting whitespace between them. – Alan H. – 2011-08-26T21:32:13.423

66

Turn off history expansion:

set +H

or

set +o histexpand

You can add one of those commands to your ~/.bashrc if you usually don't use history expansion.

Bash 4.3 added a special case:

the history expansion character is also treated as quoted if it immediately precedes the closing double quote in a double-quoted string

Paused until further notice.

Posted 2010-04-22T18:53:43.307

Reputation: 86 075

Thanks--this worked, but I had to do set +o histexpand, not set -o histexpand. Could you edit your answer to fix this? – Matthew – 2010-04-22T19:04:14.917

Oops, typo, sorry. Fixed. – Paused until further notice. – 2010-04-22T22:29:02.387

This is so awesome! echo "@AquariusPower!" – joehanna – 2016-06-30T00:28:47.847

excelent! was looking for this for AGES.... thx vm! as escape wont work... I dont want to use single quotes too... I will add this to my .bashrc thx!!, never used ! in scripts and for nothing!! it was always troubling... – Aquarius Power – 2013-07-18T15:19:49.983

13

Use single quotes (') instead of double quotes ("). Single quotes turn off all interpretation of the stuff in them, while double quotes only turn off some.

bzr commit -m 'It works!'

KeithB

Posted 2010-04-22T18:53:43.307

Reputation: 8 506

4

I just now found another way, that will at least work with echoing strings (sentences) you want to punctuate with an exclamation point. It does an end-run, more or less, around Bash histexpand and takes only a bit longer to code.

The hex for an exclamation point, as listed on http://www.ascii-code.com/, is 21, so if you put \x21 at the end of your string, echo -e $foo, make $foo its own expanded echo [ie, foo=$(echo -e "$foo")], what you get when you echo $foo again is the string with an ! at the end. And no switching histexpand either.

Works for sure in Bash 4+. Earlier versions, ymmv.

SilversleevesX

Posted 2010-04-22T18:53:43.307

Reputation: 89

I don't get what you're saying about echoing itself. 'echo -e' and 'printf' interpret escaped octal, hex, and Unicode codes. In cases where you're using either of those, you can use "\041" (the octal for the "!") or "\x21" (the hex) to stand-in for the exclamation point. For example: printf "OMG\041". If you're building a double-quoted string somewhere that isn't echo or printf, like the commit string OP asked about, you can embed $(echo -e "\041") in the string where the exclamation point should be. Like this: bzr commit -m "This is a great commit$(echo -e "\041")". – Todd Walton – 2019-01-16T20:16:55.513

Hmmm it does not work for me. – lzap – 2014-06-10T11:52:55.900