Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
1  
Doing bzr commit -m "It works"! works, too. –  kba May 18 '13 at 13:59
    
As I noted before the command you put does actually work on it's own :) bzr commit -m "It works!" –  h4unt3r Aug 18 at 3:26

5 Answers 5

up vote 53 down vote accepted

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!' 
share|improve this answer
1  
Since 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 May 24 '11 at 17:55
2  
@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 May 24 '11 at 18:17
    
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 May 24 '11 at 18:37

Old question I know, but for future searchers:

You can also use this method if you want double quotes as well as the exclamation:

echo "It's broken"'!'

Note to user N13, before you edit my answer again: This works even if the ! is not at the end of the line, thanks to the single-quotes.

Eg, this works:

echo "hello there"'!'" and goodbye"
share|improve this answer
3  
Nice tip, JWD. I didn’t realize bash strings could be concatenated simply by omitting whitespace between them. –  Alan H. Aug 26 '11 at 21:32
    
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 Aug 18 at 3:24
    
@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 Aug 18 at 21:05
    
@jwd what version of bash are you running? I always have hist expansion on. Did you accidentally use two !! for your test? –  h4unt3r Aug 19 at 0:15
    
@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 Aug 19 at 20:28

Turn off history expansion

set +H

or

set +o histexpand
share|improve this answer
    
Thanks--this worked, but I had to do set +o histexpand, not set -o histexpand. Could you edit your answer to fix this? –  Matthew Apr 22 '10 at 19:04
    
Oops, typo, sorry. Fixed. –  Dennis Williamson Apr 22 '10 at 22:29
    
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 Jul 18 '13 at 15:19

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!'
share|improve this answer

I just now found another way, that will at least work with echo'ing 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 here 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.

BZT

share|improve this answer
    
Hmmm it does not work for me. –  lzap Jun 10 at 11:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.