In bash, how do I escape an exclamation mark?












172














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






  • 1




    As I noted before the command you put does actually work on it's own :) bzr commit -m "It works!"
    – h4unt3r
    Aug 18 '14 at 3:26










  • An even quirkier case with nested quotes: stackoverflow.com/questions/22125658/…
    – Ciro Santilli 新疆改造中心 六四事件 法轮功
    Sep 2 '15 at 18:08












  • 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
    Nov 29 '15 at 10:19
















172














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






  • 1




    As I noted before the command you put does actually work on it's own :) bzr commit -m "It works!"
    – h4unt3r
    Aug 18 '14 at 3:26










  • An even quirkier case with nested quotes: stackoverflow.com/questions/22125658/…
    – Ciro Santilli 新疆改造中心 六四事件 法轮功
    Sep 2 '15 at 18:08












  • 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
    Nov 29 '15 at 10:19














172












172








172


36





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















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?







bash syntax escape-characters






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jun 28 '12 at 8:36









Sathyajith Bhat

52.6k29154252




52.6k29154252










asked Apr 22 '10 at 18:53









Matthew

6,0011562110




6,0011562110








  • 1




    Doing bzr commit -m "It works"! works, too.
    – kba
    May 18 '13 at 13:59






  • 1




    As I noted before the command you put does actually work on it's own :) bzr commit -m "It works!"
    – h4unt3r
    Aug 18 '14 at 3:26










  • An even quirkier case with nested quotes: stackoverflow.com/questions/22125658/…
    – Ciro Santilli 新疆改造中心 六四事件 法轮功
    Sep 2 '15 at 18:08












  • 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
    Nov 29 '15 at 10:19














  • 1




    Doing bzr commit -m "It works"! works, too.
    – kba
    May 18 '13 at 13:59






  • 1




    As I noted before the command you put does actually work on it's own :) bzr commit -m "It works!"
    – h4unt3r
    Aug 18 '14 at 3:26










  • An even quirkier case with nested quotes: stackoverflow.com/questions/22125658/…
    – Ciro Santilli 新疆改造中心 六四事件 法轮功
    Sep 2 '15 at 18:08












  • 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
    Nov 29 '15 at 10:19








1




1




Doing bzr commit -m "It works"! works, too.
– kba
May 18 '13 at 13:59




Doing bzr commit -m "It works"! works, too.
– kba
May 18 '13 at 13:59




1




1




As I noted before the command you put does actually work on it's own :) bzr commit -m "It works!"
– h4unt3r
Aug 18 '14 at 3:26




As I noted before the command you put does actually work on it's own :) bzr commit -m "It works!"
– h4unt3r
Aug 18 '14 at 3:26












An even quirkier case with nested quotes: stackoverflow.com/questions/22125658/…
– Ciro Santilli 新疆改造中心 六四事件 法轮功
Sep 2 '15 at 18:08






An even quirkier case with nested quotes: stackoverflow.com/questions/22125658/…
– Ciro Santilli 新疆改造中心 六四事件 法轮功
Sep 2 '15 at 18:08














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
Nov 29 '15 at 10:19




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
Nov 29 '15 at 10:19










5 Answers
5






active

oldest

votes


















142














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

















  • 23




    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






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










  • 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
    Jan 6 '18 at 10:49





















145














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.






share|improve this answer



















  • 4




    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 '14 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 '14 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 '14 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 '14 at 20:28



















65














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







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










  • This is so awesome! echo "@AquariusPower!"
    – joehanna
    Jun 30 '16 at 0:28





















10














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































    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.






    share|improve this answer























    • Hmmm it does not work for me.
      – lzap
      Jun 10 '14 at 11:52











    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "3"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f133780%2fin-bash-how-do-i-escape-an-exclamation-mark%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    5 Answers
    5






    active

    oldest

    votes








    5 Answers
    5






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    142














    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

















    • 23




      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






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










    • 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
      Jan 6 '18 at 10:49


















    142














    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

















    • 23




      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






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










    • 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
      Jan 6 '18 at 10:49
















    142












    142








    142






    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












    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












    share|improve this answer



    share|improve this answer










    answered Apr 22 '10 at 19:31









    Benjamin Bannier

    12.8k23737




    12.8k23737








    • 23




      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






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










    • 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
      Jan 6 '18 at 10:49
















    • 23




      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






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










    • 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
      Jan 6 '18 at 10:49










    23




    23




    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




    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




    7




    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
    May 24 '11 at 18:17




    @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




    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












    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
    Jan 6 '18 at 10:49






    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
    Jan 6 '18 at 10:49















    145














    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.






    share|improve this answer



















    • 4




      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 '14 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 '14 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 '14 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 '14 at 20:28
















    145














    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.






    share|improve this answer



















    • 4




      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 '14 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 '14 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 '14 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 '14 at 20:28














    145












    145








    145






    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.






    share|improve this answer














    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.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 2 days ago

























    answered Jun 23 '11 at 19:16









    jwd

    2,22721513




    2,22721513








    • 4




      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 '14 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 '14 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 '14 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 '14 at 20:28














    • 4




      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 '14 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 '14 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 '14 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 '14 at 20:28








    4




    4




    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




    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 '14 at 3:24




    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 '14 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 '14 at 21:05




    @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 '14 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 '14 at 0:15






    @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 '14 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 '14 at 20:28




    @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 '14 at 20:28











    65














    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







    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










    • This is so awesome! echo "@AquariusPower!"
      – joehanna
      Jun 30 '16 at 0:28


















    65














    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







    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










    • This is so awesome! echo "@AquariusPower!"
      – joehanna
      Jun 30 '16 at 0:28
















    65












    65








    65






    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







    share|improve this answer














    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








    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Dec 6 '18 at 20:22

























    answered Apr 22 '10 at 18:58









    Dennis Williamson

    76.2k14129167




    76.2k14129167












    • 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










    • This is so awesome! echo "@AquariusPower!"
      – joehanna
      Jun 30 '16 at 0:28




















    • 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










    • This is so awesome! echo "@AquariusPower!"
      – joehanna
      Jun 30 '16 at 0:28


















    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




    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




    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




    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












    This is so awesome! echo "@AquariusPower!"
    – joehanna
    Jun 30 '16 at 0:28






    This is so awesome! echo "@AquariusPower!"
    – joehanna
    Jun 30 '16 at 0:28













    10














    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




























      10














      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


























        10












        10








        10






        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














        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














        share|improve this answer



        share|improve this answer








        edited Apr 23 '10 at 12:14

























        answered Apr 22 '10 at 19:32









        KeithB

        7,70611813




        7,70611813























            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.






            share|improve this answer























            • Hmmm it does not work for me.
              – lzap
              Jun 10 '14 at 11:52
















            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.






            share|improve this answer























            • Hmmm it does not work for me.
              – lzap
              Jun 10 '14 at 11:52














            4












            4








            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.






            share|improve this answer














            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.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Sep 28 '17 at 12:07









            Daniel Böhmer

            4571314




            4571314










            answered Nov 23 '11 at 4:28









            SilversleevesX

            893




            893












            • Hmmm it does not work for me.
              – lzap
              Jun 10 '14 at 11:52


















            • Hmmm it does not work for me.
              – lzap
              Jun 10 '14 at 11:52
















            Hmmm it does not work for me.
            – lzap
            Jun 10 '14 at 11:52




            Hmmm it does not work for me.
            – lzap
            Jun 10 '14 at 11:52


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Super User!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f133780%2fin-bash-how-do-i-escape-an-exclamation-mark%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to make a Squid Proxy server?

            Is this a new Fibonacci Identity?

            19世紀