why grep “!” shows “bash: !: event not found”?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












If I run



grep "!" test.txt


it will show error




bash: !: event not found




Well, I know I actually should use grep '!'. But how to understand the above error?



I know ! is bash special character. According to http://mywiki.wooledge.org/Quotes.




Double quotes: "..." prevents some substitutions but allows others.
Every substitution that begins with a dollar sign $ is performed, as
is the legacy ... (backtick) command substitution. Backslash
escaping is also performed. No word splitting or filename expansion is
performed.




It doesn't mention that double quotes will affect !. Could somebody explain how does bash or grep interpret grep "!"? What is the "event"?










share|improve this question

























    up vote
    1
    down vote

    favorite












    If I run



    grep "!" test.txt


    it will show error




    bash: !: event not found




    Well, I know I actually should use grep '!'. But how to understand the above error?



    I know ! is bash special character. According to http://mywiki.wooledge.org/Quotes.




    Double quotes: "..." prevents some substitutions but allows others.
    Every substitution that begins with a dollar sign $ is performed, as
    is the legacy ... (backtick) command substitution. Backslash
    escaping is also performed. No word splitting or filename expansion is
    performed.




    It doesn't mention that double quotes will affect !. Could somebody explain how does bash or grep interpret grep "!"? What is the "event"?










    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      If I run



      grep "!" test.txt


      it will show error




      bash: !: event not found




      Well, I know I actually should use grep '!'. But how to understand the above error?



      I know ! is bash special character. According to http://mywiki.wooledge.org/Quotes.




      Double quotes: "..." prevents some substitutions but allows others.
      Every substitution that begins with a dollar sign $ is performed, as
      is the legacy ... (backtick) command substitution. Backslash
      escaping is also performed. No word splitting or filename expansion is
      performed.




      It doesn't mention that double quotes will affect !. Could somebody explain how does bash or grep interpret grep "!"? What is the "event"?










      share|improve this question













      If I run



      grep "!" test.txt


      it will show error




      bash: !: event not found




      Well, I know I actually should use grep '!'. But how to understand the above error?



      I know ! is bash special character. According to http://mywiki.wooledge.org/Quotes.




      Double quotes: "..." prevents some substitutions but allows others.
      Every substitution that begins with a dollar sign $ is performed, as
      is the legacy ... (backtick) command substitution. Backslash
      escaping is also performed. No word splitting or filename expansion is
      performed.




      It doesn't mention that double quotes will affect !. Could somebody explain how does bash or grep interpret grep "!"? What is the "event"?







      bash grep special-characters






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 20 '17 at 15:44









      user15964

      2981516




      2981516




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          4
          down vote













          It's not grep that's causing it. It's your bash that is interpreting the !. Switch to a newer version of bash or use single quotes '!' to shut up bash.






          share|improve this answer




















          • why new version has no error? Bug or some new feature?
            – user15964
            Mar 20 '17 at 15:55











          • Actually in your scenario, grep ! test.txt would work as well. I guess it's a bug that's why this changed behavior.
            – user218374
            Mar 20 '17 at 16:00










          • Ok, grep '!' works, Why bash -c 'grep '!' test.txt' doesn't work?
            – user15964
            Mar 20 '17 at 16:05










          • Actually you need to realize how quoting works, single quoting , that is, in your above case. You can't slip in a single quote inside of single quotes just by escaping it. Why? Coz, the backslash ain't special inside of single quotes, the way it is special inside double quotes or in empty space. You shoud do this: bash -c 'grep '''!''' test.txt'
            – user218374
            Mar 20 '17 at 16:21










          • The History Expansion feature is still available and enabled by default as of Bash 4.4.19, and I don't expect it to be removed soon. Quote it or put set +o histexpand in your .bashrc or equivalent.
            – arielCo
            11 mins ago


















          up vote
          0
          down vote













          You're looking at Bash's History Expansion feature. Some find it handy to recall previous commands, but I just put set +o histexpand in my .bashrc and press Ctrl+R to search for part of the command.






          share|improve this answer




















            Your Answer








            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "106"
            ;
            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',
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            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%2funix.stackexchange.com%2fquestions%2f352631%2fwhy-grep-shows-bash-event-not-found%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            4
            down vote













            It's not grep that's causing it. It's your bash that is interpreting the !. Switch to a newer version of bash or use single quotes '!' to shut up bash.






            share|improve this answer




















            • why new version has no error? Bug or some new feature?
              – user15964
              Mar 20 '17 at 15:55











            • Actually in your scenario, grep ! test.txt would work as well. I guess it's a bug that's why this changed behavior.
              – user218374
              Mar 20 '17 at 16:00










            • Ok, grep '!' works, Why bash -c 'grep '!' test.txt' doesn't work?
              – user15964
              Mar 20 '17 at 16:05










            • Actually you need to realize how quoting works, single quoting , that is, in your above case. You can't slip in a single quote inside of single quotes just by escaping it. Why? Coz, the backslash ain't special inside of single quotes, the way it is special inside double quotes or in empty space. You shoud do this: bash -c 'grep '''!''' test.txt'
              – user218374
              Mar 20 '17 at 16:21










            • The History Expansion feature is still available and enabled by default as of Bash 4.4.19, and I don't expect it to be removed soon. Quote it or put set +o histexpand in your .bashrc or equivalent.
              – arielCo
              11 mins ago















            up vote
            4
            down vote













            It's not grep that's causing it. It's your bash that is interpreting the !. Switch to a newer version of bash or use single quotes '!' to shut up bash.






            share|improve this answer




















            • why new version has no error? Bug or some new feature?
              – user15964
              Mar 20 '17 at 15:55











            • Actually in your scenario, grep ! test.txt would work as well. I guess it's a bug that's why this changed behavior.
              – user218374
              Mar 20 '17 at 16:00










            • Ok, grep '!' works, Why bash -c 'grep '!' test.txt' doesn't work?
              – user15964
              Mar 20 '17 at 16:05










            • Actually you need to realize how quoting works, single quoting , that is, in your above case. You can't slip in a single quote inside of single quotes just by escaping it. Why? Coz, the backslash ain't special inside of single quotes, the way it is special inside double quotes or in empty space. You shoud do this: bash -c 'grep '''!''' test.txt'
              – user218374
              Mar 20 '17 at 16:21










            • The History Expansion feature is still available and enabled by default as of Bash 4.4.19, and I don't expect it to be removed soon. Quote it or put set +o histexpand in your .bashrc or equivalent.
              – arielCo
              11 mins ago













            up vote
            4
            down vote










            up vote
            4
            down vote









            It's not grep that's causing it. It's your bash that is interpreting the !. Switch to a newer version of bash or use single quotes '!' to shut up bash.






            share|improve this answer












            It's not grep that's causing it. It's your bash that is interpreting the !. Switch to a newer version of bash or use single quotes '!' to shut up bash.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 20 '17 at 15:49







            user218374


















            • why new version has no error? Bug or some new feature?
              – user15964
              Mar 20 '17 at 15:55











            • Actually in your scenario, grep ! test.txt would work as well. I guess it's a bug that's why this changed behavior.
              – user218374
              Mar 20 '17 at 16:00










            • Ok, grep '!' works, Why bash -c 'grep '!' test.txt' doesn't work?
              – user15964
              Mar 20 '17 at 16:05










            • Actually you need to realize how quoting works, single quoting , that is, in your above case. You can't slip in a single quote inside of single quotes just by escaping it. Why? Coz, the backslash ain't special inside of single quotes, the way it is special inside double quotes or in empty space. You shoud do this: bash -c 'grep '''!''' test.txt'
              – user218374
              Mar 20 '17 at 16:21










            • The History Expansion feature is still available and enabled by default as of Bash 4.4.19, and I don't expect it to be removed soon. Quote it or put set +o histexpand in your .bashrc or equivalent.
              – arielCo
              11 mins ago

















            • why new version has no error? Bug or some new feature?
              – user15964
              Mar 20 '17 at 15:55











            • Actually in your scenario, grep ! test.txt would work as well. I guess it's a bug that's why this changed behavior.
              – user218374
              Mar 20 '17 at 16:00










            • Ok, grep '!' works, Why bash -c 'grep '!' test.txt' doesn't work?
              – user15964
              Mar 20 '17 at 16:05










            • Actually you need to realize how quoting works, single quoting , that is, in your above case. You can't slip in a single quote inside of single quotes just by escaping it. Why? Coz, the backslash ain't special inside of single quotes, the way it is special inside double quotes or in empty space. You shoud do this: bash -c 'grep '''!''' test.txt'
              – user218374
              Mar 20 '17 at 16:21










            • The History Expansion feature is still available and enabled by default as of Bash 4.4.19, and I don't expect it to be removed soon. Quote it or put set +o histexpand in your .bashrc or equivalent.
              – arielCo
              11 mins ago
















            why new version has no error? Bug or some new feature?
            – user15964
            Mar 20 '17 at 15:55





            why new version has no error? Bug or some new feature?
            – user15964
            Mar 20 '17 at 15:55













            Actually in your scenario, grep ! test.txt would work as well. I guess it's a bug that's why this changed behavior.
            – user218374
            Mar 20 '17 at 16:00




            Actually in your scenario, grep ! test.txt would work as well. I guess it's a bug that's why this changed behavior.
            – user218374
            Mar 20 '17 at 16:00












            Ok, grep '!' works, Why bash -c 'grep '!' test.txt' doesn't work?
            – user15964
            Mar 20 '17 at 16:05




            Ok, grep '!' works, Why bash -c 'grep '!' test.txt' doesn't work?
            – user15964
            Mar 20 '17 at 16:05












            Actually you need to realize how quoting works, single quoting , that is, in your above case. You can't slip in a single quote inside of single quotes just by escaping it. Why? Coz, the backslash ain't special inside of single quotes, the way it is special inside double quotes or in empty space. You shoud do this: bash -c 'grep '''!''' test.txt'
            – user218374
            Mar 20 '17 at 16:21




            Actually you need to realize how quoting works, single quoting , that is, in your above case. You can't slip in a single quote inside of single quotes just by escaping it. Why? Coz, the backslash ain't special inside of single quotes, the way it is special inside double quotes or in empty space. You shoud do this: bash -c 'grep '''!''' test.txt'
            – user218374
            Mar 20 '17 at 16:21












            The History Expansion feature is still available and enabled by default as of Bash 4.4.19, and I don't expect it to be removed soon. Quote it or put set +o histexpand in your .bashrc or equivalent.
            – arielCo
            11 mins ago





            The History Expansion feature is still available and enabled by default as of Bash 4.4.19, and I don't expect it to be removed soon. Quote it or put set +o histexpand in your .bashrc or equivalent.
            – arielCo
            11 mins ago













            up vote
            0
            down vote













            You're looking at Bash's History Expansion feature. Some find it handy to recall previous commands, but I just put set +o histexpand in my .bashrc and press Ctrl+R to search for part of the command.






            share|improve this answer
























              up vote
              0
              down vote













              You're looking at Bash's History Expansion feature. Some find it handy to recall previous commands, but I just put set +o histexpand in my .bashrc and press Ctrl+R to search for part of the command.






              share|improve this answer






















                up vote
                0
                down vote










                up vote
                0
                down vote









                You're looking at Bash's History Expansion feature. Some find it handy to recall previous commands, but I just put set +o histexpand in my .bashrc and press Ctrl+R to search for part of the command.






                share|improve this answer












                You're looking at Bash's History Expansion feature. Some find it handy to recall previous commands, but I just put set +o histexpand in my .bashrc and press Ctrl+R to search for part of the command.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 13 mins ago









                arielCo

                738510




                738510



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f352631%2fwhy-grep-shows-bash-event-not-found%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    How to check contact read email or not when send email to Individual?

                    Bahrain

                    Postfix configuration issue with fips on centos 7; mailgun relay