How to echo a bang!

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












55















I tried to create a script by echo'ing the contents into a file, instead of opening it with a editor



echo -e "#!/bin/bash n /usr/bin/command args" > .scripts/command


The output:




bash: !/bin/bash: event not found




I've isolated this strange behavior to the bang.



$ echo !
!

$ echo "!"
bash: !: event not found

$ echo #!
#!

$ echo #!/bin/bash
bash: !/bin/bash: event not found


  • Why is bang causing this?

  • What are these "events" that bash refers to?

  • How do I get past this problem and print "#!/bin/bash" to the screen or my file?









share|improve this question


























    55















    I tried to create a script by echo'ing the contents into a file, instead of opening it with a editor



    echo -e "#!/bin/bash n /usr/bin/command args" > .scripts/command


    The output:




    bash: !/bin/bash: event not found




    I've isolated this strange behavior to the bang.



    $ echo !
    !

    $ echo "!"
    bash: !: event not found

    $ echo #!
    #!

    $ echo #!/bin/bash
    bash: !/bin/bash: event not found


    • Why is bang causing this?

    • What are these "events" that bash refers to?

    • How do I get past this problem and print "#!/bin/bash" to the screen or my file?









    share|improve this question
























      55












      55








      55


      11






      I tried to create a script by echo'ing the contents into a file, instead of opening it with a editor



      echo -e "#!/bin/bash n /usr/bin/command args" > .scripts/command


      The output:




      bash: !/bin/bash: event not found




      I've isolated this strange behavior to the bang.



      $ echo !
      !

      $ echo "!"
      bash: !: event not found

      $ echo #!
      #!

      $ echo #!/bin/bash
      bash: !/bin/bash: event not found


      • Why is bang causing this?

      • What are these "events" that bash refers to?

      • How do I get past this problem and print "#!/bin/bash" to the screen or my file?









      share|improve this question














      I tried to create a script by echo'ing the contents into a file, instead of opening it with a editor



      echo -e "#!/bin/bash n /usr/bin/command args" > .scripts/command


      The output:




      bash: !/bin/bash: event not found




      I've isolated this strange behavior to the bang.



      $ echo !
      !

      $ echo "!"
      bash: !: event not found

      $ echo #!
      #!

      $ echo #!/bin/bash
      bash: !/bin/bash: event not found


      • Why is bang causing this?

      • What are these "events" that bash refers to?

      • How do I get past this problem and print "#!/bin/bash" to the screen or my file?






      command-line bash echo






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Oct 13 '10 at 8:44









      StefanStefan

      11.5k3183123




      11.5k3183123




















          6 Answers
          6






          active

          oldest

          votes


















          56














          Try using single quotes.



          echo -e '#!/bin/bash n /usr/bin/command args' > .scripts/command

          echo '#!'

          echo '#!/bin/bash'


          The problem is occurring because bash is searching its history for !/bin/bash. Using single quotes escapes this behaviour.






          share|improve this answer




















          • 2





            It also disables string interpolation :(

            – Hubro
            Jan 23 '15 at 7:53











          • That's the point! You can add other arguments to the same echo command using double quotes and get interpolation on those parts.

            – Richm
            Jan 23 '15 at 11:32






          • 3





            you can also use C style string in bash with $''. For example echo $'1!n2!n3!n' prints each number followed by a bang and a newline.

            – spelufo
            Feb 19 '15 at 14:34






          • 1





            Putting bang in single quotes didn’t help me when entering a multi-line string: !bash—single quotes do not escape bang (for real)

            – binki
            Mar 7 '16 at 17:03







          • 1





            @kbolino You are right. So the full example string would be: echo '0123'"$var"'456', note two pairs of single quotes and one pair of double quotes.

            – phyatt
            Apr 9 '18 at 19:26


















          16














          As Richm said, bash is trying to do a history match. Another way to avoid it is to just escape the bang with a :



          $ echo #!/bin/bash
          #!/bin/bash


          Though beware that inside double quotes, the is not removed:



          $ echo "!"
          !





          share|improve this answer




















          • 7





            In bash, "!" actually expands to !, not !, supposedly for POSIX compliance.

            – Mikel
            Apr 7 '11 at 23:09












          • @Mikel Sigh. So many differences between zsh and bash

            – Michael Mrozek
            Apr 8 '11 at 3:55











          • This works even when entering a multiline string. Remembering to 1. be outside of a string when entering the bang and 2. use this method (backslash) seems to work with the least surprise in most scenarios.

            – binki
            Mar 7 '16 at 17:09











          • Would be useful to note that bash doesn’t do history searches when executing a script, so no need to do anything special for it there.

            – binki
            Mar 7 '16 at 17:09


















          13














          ! starts a history substitution (an “event” is a line in the command history); for example !ls expands to the last command beginning with ls, and !?foo expands to the last command containing foo. You can also extract specific words (e.g. !!:1 refers to the first word of the previous command) and more; see the manual for details.



          This feature was invented to quickly recall previous commands in the days when command line edition was primitive. With modern shells (at least bash and zsh) and copy-and-paste, history expansion is not as useful as it used to be — it's still helpful, but you can get by without it.



          You can change which character triggers history substitution by setting the histchars variable; if you rarely use history substitution, you can set e.g. histchars='¡^' so that ¡ triggers history expansion instead of !. You can even turn off the feature altogether with set +o histexpand.






          share|improve this answer






























            3














            To be able to disable history expansion on a particular command line, you can use space as the 3rd character of $histchars:



            histchars='!^ '


            Then, if you enter your command with a leading space, history expansion will not be performed.



            bash-4.3$ echo "#!/bin/bash"
            bash: !/bin/bash: event not found
            bash-4.3$ echo "#!/bin/bash"
            #!/bin/bash


            Note however that leading spaces are also used when $HISTCONTROL contains ignorespace as a way to tell bash not to record a command line in the history.



            If you want both features indenpendantly, you'll need to choose another character as the 3rd character of $histchars. You want one that doesn't affect the way your command is interpreted. A few options:



            • using backslash (): echo foo works but that has the side effect of disabling aliases or keywords.

            • TAB: to insert it in first position, you need to press Ctrl+VTab though.


            • if you don't mind typing two keys, you can pick any character that normally doesn't appear in first position (%, @, ?, pick your own) and make an empty alias for it:



              histchars='!^%'
              alias %=


              Then enter that character, space and your command:



              bash-4.3$ % echo !!
              !!


            (you won't be able not to record a command where history substitution has been disabled though. Also note that the default 3rd character of $histchars is # so that history expansion is not done in comments. If you change it, and you enter comments at the prompt, you should be aware of the fact that ! sequences may be expanded there).






            share|improve this answer






























              2














              The proposed solutions don't work in, e.g., the following example:



              $ bash -c "echo 'hello World!'"
              -bash: !'": event not found
              $


              In this case the bang can be printed using its octal ASCII code:



              $ bash -c "echo -e 'hello World041'"
              hello World!
              $





              share|improve this answer




















              • 1





                In your first command, the ! is between double quotes, where as other answers indicate it retains its history expansion meaning. You could use bash -c 'echo '''hello World!'' or bash -c "echo 'hello World"!"'".

                – Gilles
                Dec 26 '10 at 11:18











              • Without the -i parameter, the environment may be altered (like your ~/.profile doesn't run). However, doing -i means any output from your .profile will be captured in the output of the command you actually want to run.

                – Brent
                Apr 6 '15 at 21:18


















              -1














              Ever since Bash 4.3, you can now use double quotes to quote the history expansion character:



              $ bash --version
              GNU bash, version 4.3...
              [...]
              $ echo "Hello World!"
              Hello World!





              share|improve this answer


















              • 4





                No, that's only ! followed by " that is no longer special. echo "foo!" is OK, echo "foo!bar" is not

                – Stéphane Chazelas
                Sep 16 '16 at 13:07










              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',
              autoActivateHeartbeat: false,
              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%2f3051%2fhow-to-echo-a-bang%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              6 Answers
              6






              active

              oldest

              votes








              6 Answers
              6






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              56














              Try using single quotes.



              echo -e '#!/bin/bash n /usr/bin/command args' > .scripts/command

              echo '#!'

              echo '#!/bin/bash'


              The problem is occurring because bash is searching its history for !/bin/bash. Using single quotes escapes this behaviour.






              share|improve this answer




















              • 2





                It also disables string interpolation :(

                – Hubro
                Jan 23 '15 at 7:53











              • That's the point! You can add other arguments to the same echo command using double quotes and get interpolation on those parts.

                – Richm
                Jan 23 '15 at 11:32






              • 3





                you can also use C style string in bash with $''. For example echo $'1!n2!n3!n' prints each number followed by a bang and a newline.

                – spelufo
                Feb 19 '15 at 14:34






              • 1





                Putting bang in single quotes didn’t help me when entering a multi-line string: !bash—single quotes do not escape bang (for real)

                – binki
                Mar 7 '16 at 17:03







              • 1





                @kbolino You are right. So the full example string would be: echo '0123'"$var"'456', note two pairs of single quotes and one pair of double quotes.

                – phyatt
                Apr 9 '18 at 19:26















              56














              Try using single quotes.



              echo -e '#!/bin/bash n /usr/bin/command args' > .scripts/command

              echo '#!'

              echo '#!/bin/bash'


              The problem is occurring because bash is searching its history for !/bin/bash. Using single quotes escapes this behaviour.






              share|improve this answer




















              • 2





                It also disables string interpolation :(

                – Hubro
                Jan 23 '15 at 7:53











              • That's the point! You can add other arguments to the same echo command using double quotes and get interpolation on those parts.

                – Richm
                Jan 23 '15 at 11:32






              • 3





                you can also use C style string in bash with $''. For example echo $'1!n2!n3!n' prints each number followed by a bang and a newline.

                – spelufo
                Feb 19 '15 at 14:34






              • 1





                Putting bang in single quotes didn’t help me when entering a multi-line string: !bash—single quotes do not escape bang (for real)

                – binki
                Mar 7 '16 at 17:03







              • 1





                @kbolino You are right. So the full example string would be: echo '0123'"$var"'456', note two pairs of single quotes and one pair of double quotes.

                – phyatt
                Apr 9 '18 at 19:26













              56












              56








              56







              Try using single quotes.



              echo -e '#!/bin/bash n /usr/bin/command args' > .scripts/command

              echo '#!'

              echo '#!/bin/bash'


              The problem is occurring because bash is searching its history for !/bin/bash. Using single quotes escapes this behaviour.






              share|improve this answer















              Try using single quotes.



              echo -e '#!/bin/bash n /usr/bin/command args' > .scripts/command

              echo '#!'

              echo '#!/bin/bash'


              The problem is occurring because bash is searching its history for !/bin/bash. Using single quotes escapes this behaviour.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Oct 13 '10 at 9:24

























              answered Oct 13 '10 at 9:17









              RichmRichm

              3,1541612




              3,1541612







              • 2





                It also disables string interpolation :(

                – Hubro
                Jan 23 '15 at 7:53











              • That's the point! You can add other arguments to the same echo command using double quotes and get interpolation on those parts.

                – Richm
                Jan 23 '15 at 11:32






              • 3





                you can also use C style string in bash with $''. For example echo $'1!n2!n3!n' prints each number followed by a bang and a newline.

                – spelufo
                Feb 19 '15 at 14:34






              • 1





                Putting bang in single quotes didn’t help me when entering a multi-line string: !bash—single quotes do not escape bang (for real)

                – binki
                Mar 7 '16 at 17:03







              • 1





                @kbolino You are right. So the full example string would be: echo '0123'"$var"'456', note two pairs of single quotes and one pair of double quotes.

                – phyatt
                Apr 9 '18 at 19:26












              • 2





                It also disables string interpolation :(

                – Hubro
                Jan 23 '15 at 7:53











              • That's the point! You can add other arguments to the same echo command using double quotes and get interpolation on those parts.

                – Richm
                Jan 23 '15 at 11:32






              • 3





                you can also use C style string in bash with $''. For example echo $'1!n2!n3!n' prints each number followed by a bang and a newline.

                – spelufo
                Feb 19 '15 at 14:34






              • 1





                Putting bang in single quotes didn’t help me when entering a multi-line string: !bash—single quotes do not escape bang (for real)

                – binki
                Mar 7 '16 at 17:03







              • 1





                @kbolino You are right. So the full example string would be: echo '0123'"$var"'456', note two pairs of single quotes and one pair of double quotes.

                – phyatt
                Apr 9 '18 at 19:26







              2




              2





              It also disables string interpolation :(

              – Hubro
              Jan 23 '15 at 7:53





              It also disables string interpolation :(

              – Hubro
              Jan 23 '15 at 7:53













              That's the point! You can add other arguments to the same echo command using double quotes and get interpolation on those parts.

              – Richm
              Jan 23 '15 at 11:32





              That's the point! You can add other arguments to the same echo command using double quotes and get interpolation on those parts.

              – Richm
              Jan 23 '15 at 11:32




              3




              3





              you can also use C style string in bash with $''. For example echo $'1!n2!n3!n' prints each number followed by a bang and a newline.

              – spelufo
              Feb 19 '15 at 14:34





              you can also use C style string in bash with $''. For example echo $'1!n2!n3!n' prints each number followed by a bang and a newline.

              – spelufo
              Feb 19 '15 at 14:34




              1




              1





              Putting bang in single quotes didn’t help me when entering a multi-line string: !bash—single quotes do not escape bang (for real)

              – binki
              Mar 7 '16 at 17:03






              Putting bang in single quotes didn’t help me when entering a multi-line string: !bash—single quotes do not escape bang (for real)

              – binki
              Mar 7 '16 at 17:03





              1




              1





              @kbolino You are right. So the full example string would be: echo '0123'"$var"'456', note two pairs of single quotes and one pair of double quotes.

              – phyatt
              Apr 9 '18 at 19:26





              @kbolino You are right. So the full example string would be: echo '0123'"$var"'456', note two pairs of single quotes and one pair of double quotes.

              – phyatt
              Apr 9 '18 at 19:26













              16














              As Richm said, bash is trying to do a history match. Another way to avoid it is to just escape the bang with a :



              $ echo #!/bin/bash
              #!/bin/bash


              Though beware that inside double quotes, the is not removed:



              $ echo "!"
              !





              share|improve this answer




















              • 7





                In bash, "!" actually expands to !, not !, supposedly for POSIX compliance.

                – Mikel
                Apr 7 '11 at 23:09












              • @Mikel Sigh. So many differences between zsh and bash

                – Michael Mrozek
                Apr 8 '11 at 3:55











              • This works even when entering a multiline string. Remembering to 1. be outside of a string when entering the bang and 2. use this method (backslash) seems to work with the least surprise in most scenarios.

                – binki
                Mar 7 '16 at 17:09











              • Would be useful to note that bash doesn’t do history searches when executing a script, so no need to do anything special for it there.

                – binki
                Mar 7 '16 at 17:09















              16














              As Richm said, bash is trying to do a history match. Another way to avoid it is to just escape the bang with a :



              $ echo #!/bin/bash
              #!/bin/bash


              Though beware that inside double quotes, the is not removed:



              $ echo "!"
              !





              share|improve this answer




















              • 7





                In bash, "!" actually expands to !, not !, supposedly for POSIX compliance.

                – Mikel
                Apr 7 '11 at 23:09












              • @Mikel Sigh. So many differences between zsh and bash

                – Michael Mrozek
                Apr 8 '11 at 3:55











              • This works even when entering a multiline string. Remembering to 1. be outside of a string when entering the bang and 2. use this method (backslash) seems to work with the least surprise in most scenarios.

                – binki
                Mar 7 '16 at 17:09











              • Would be useful to note that bash doesn’t do history searches when executing a script, so no need to do anything special for it there.

                – binki
                Mar 7 '16 at 17:09













              16












              16








              16







              As Richm said, bash is trying to do a history match. Another way to avoid it is to just escape the bang with a :



              $ echo #!/bin/bash
              #!/bin/bash


              Though beware that inside double quotes, the is not removed:



              $ echo "!"
              !





              share|improve this answer















              As Richm said, bash is trying to do a history match. Another way to avoid it is to just escape the bang with a :



              $ echo #!/bin/bash
              #!/bin/bash


              Though beware that inside double quotes, the is not removed:



              $ echo "!"
              !






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Apr 13 '17 at 12:37









              Community

              1




              1










              answered Oct 13 '10 at 13:58









              Michael MrozekMichael Mrozek

              60.9k29187208




              60.9k29187208







              • 7





                In bash, "!" actually expands to !, not !, supposedly for POSIX compliance.

                – Mikel
                Apr 7 '11 at 23:09












              • @Mikel Sigh. So many differences between zsh and bash

                – Michael Mrozek
                Apr 8 '11 at 3:55











              • This works even when entering a multiline string. Remembering to 1. be outside of a string when entering the bang and 2. use this method (backslash) seems to work with the least surprise in most scenarios.

                – binki
                Mar 7 '16 at 17:09











              • Would be useful to note that bash doesn’t do history searches when executing a script, so no need to do anything special for it there.

                – binki
                Mar 7 '16 at 17:09












              • 7





                In bash, "!" actually expands to !, not !, supposedly for POSIX compliance.

                – Mikel
                Apr 7 '11 at 23:09












              • @Mikel Sigh. So many differences between zsh and bash

                – Michael Mrozek
                Apr 8 '11 at 3:55











              • This works even when entering a multiline string. Remembering to 1. be outside of a string when entering the bang and 2. use this method (backslash) seems to work with the least surprise in most scenarios.

                – binki
                Mar 7 '16 at 17:09











              • Would be useful to note that bash doesn’t do history searches when executing a script, so no need to do anything special for it there.

                – binki
                Mar 7 '16 at 17:09







              7




              7





              In bash, "!" actually expands to !, not !, supposedly for POSIX compliance.

              – Mikel
              Apr 7 '11 at 23:09






              In bash, "!" actually expands to !, not !, supposedly for POSIX compliance.

              – Mikel
              Apr 7 '11 at 23:09














              @Mikel Sigh. So many differences between zsh and bash

              – Michael Mrozek
              Apr 8 '11 at 3:55





              @Mikel Sigh. So many differences between zsh and bash

              – Michael Mrozek
              Apr 8 '11 at 3:55













              This works even when entering a multiline string. Remembering to 1. be outside of a string when entering the bang and 2. use this method (backslash) seems to work with the least surprise in most scenarios.

              – binki
              Mar 7 '16 at 17:09





              This works even when entering a multiline string. Remembering to 1. be outside of a string when entering the bang and 2. use this method (backslash) seems to work with the least surprise in most scenarios.

              – binki
              Mar 7 '16 at 17:09













              Would be useful to note that bash doesn’t do history searches when executing a script, so no need to do anything special for it there.

              – binki
              Mar 7 '16 at 17:09





              Would be useful to note that bash doesn’t do history searches when executing a script, so no need to do anything special for it there.

              – binki
              Mar 7 '16 at 17:09











              13














              ! starts a history substitution (an “event” is a line in the command history); for example !ls expands to the last command beginning with ls, and !?foo expands to the last command containing foo. You can also extract specific words (e.g. !!:1 refers to the first word of the previous command) and more; see the manual for details.



              This feature was invented to quickly recall previous commands in the days when command line edition was primitive. With modern shells (at least bash and zsh) and copy-and-paste, history expansion is not as useful as it used to be — it's still helpful, but you can get by without it.



              You can change which character triggers history substitution by setting the histchars variable; if you rarely use history substitution, you can set e.g. histchars='¡^' so that ¡ triggers history expansion instead of !. You can even turn off the feature altogether with set +o histexpand.






              share|improve this answer



























                13














                ! starts a history substitution (an “event” is a line in the command history); for example !ls expands to the last command beginning with ls, and !?foo expands to the last command containing foo. You can also extract specific words (e.g. !!:1 refers to the first word of the previous command) and more; see the manual for details.



                This feature was invented to quickly recall previous commands in the days when command line edition was primitive. With modern shells (at least bash and zsh) and copy-and-paste, history expansion is not as useful as it used to be — it's still helpful, but you can get by without it.



                You can change which character triggers history substitution by setting the histchars variable; if you rarely use history substitution, you can set e.g. histchars='¡^' so that ¡ triggers history expansion instead of !. You can even turn off the feature altogether with set +o histexpand.






                share|improve this answer

























                  13












                  13








                  13







                  ! starts a history substitution (an “event” is a line in the command history); for example !ls expands to the last command beginning with ls, and !?foo expands to the last command containing foo. You can also extract specific words (e.g. !!:1 refers to the first word of the previous command) and more; see the manual for details.



                  This feature was invented to quickly recall previous commands in the days when command line edition was primitive. With modern shells (at least bash and zsh) and copy-and-paste, history expansion is not as useful as it used to be — it's still helpful, but you can get by without it.



                  You can change which character triggers history substitution by setting the histchars variable; if you rarely use history substitution, you can set e.g. histchars='¡^' so that ¡ triggers history expansion instead of !. You can even turn off the feature altogether with set +o histexpand.






                  share|improve this answer













                  ! starts a history substitution (an “event” is a line in the command history); for example !ls expands to the last command beginning with ls, and !?foo expands to the last command containing foo. You can also extract specific words (e.g. !!:1 refers to the first word of the previous command) and more; see the manual for details.



                  This feature was invented to quickly recall previous commands in the days when command line edition was primitive. With modern shells (at least bash and zsh) and copy-and-paste, history expansion is not as useful as it used to be — it's still helpful, but you can get by without it.



                  You can change which character triggers history substitution by setting the histchars variable; if you rarely use history substitution, you can set e.g. histchars='¡^' so that ¡ triggers history expansion instead of !. You can even turn off the feature altogether with set +o histexpand.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Oct 13 '10 at 18:33









                  GillesGilles

                  531k12810631592




                  531k12810631592





















                      3














                      To be able to disable history expansion on a particular command line, you can use space as the 3rd character of $histchars:



                      histchars='!^ '


                      Then, if you enter your command with a leading space, history expansion will not be performed.



                      bash-4.3$ echo "#!/bin/bash"
                      bash: !/bin/bash: event not found
                      bash-4.3$ echo "#!/bin/bash"
                      #!/bin/bash


                      Note however that leading spaces are also used when $HISTCONTROL contains ignorespace as a way to tell bash not to record a command line in the history.



                      If you want both features indenpendantly, you'll need to choose another character as the 3rd character of $histchars. You want one that doesn't affect the way your command is interpreted. A few options:



                      • using backslash (): echo foo works but that has the side effect of disabling aliases or keywords.

                      • TAB: to insert it in first position, you need to press Ctrl+VTab though.


                      • if you don't mind typing two keys, you can pick any character that normally doesn't appear in first position (%, @, ?, pick your own) and make an empty alias for it:



                        histchars='!^%'
                        alias %=


                        Then enter that character, space and your command:



                        bash-4.3$ % echo !!
                        !!


                      (you won't be able not to record a command where history substitution has been disabled though. Also note that the default 3rd character of $histchars is # so that history expansion is not done in comments. If you change it, and you enter comments at the prompt, you should be aware of the fact that ! sequences may be expanded there).






                      share|improve this answer



























                        3














                        To be able to disable history expansion on a particular command line, you can use space as the 3rd character of $histchars:



                        histchars='!^ '


                        Then, if you enter your command with a leading space, history expansion will not be performed.



                        bash-4.3$ echo "#!/bin/bash"
                        bash: !/bin/bash: event not found
                        bash-4.3$ echo "#!/bin/bash"
                        #!/bin/bash


                        Note however that leading spaces are also used when $HISTCONTROL contains ignorespace as a way to tell bash not to record a command line in the history.



                        If you want both features indenpendantly, you'll need to choose another character as the 3rd character of $histchars. You want one that doesn't affect the way your command is interpreted. A few options:



                        • using backslash (): echo foo works but that has the side effect of disabling aliases or keywords.

                        • TAB: to insert it in first position, you need to press Ctrl+VTab though.


                        • if you don't mind typing two keys, you can pick any character that normally doesn't appear in first position (%, @, ?, pick your own) and make an empty alias for it:



                          histchars='!^%'
                          alias %=


                          Then enter that character, space and your command:



                          bash-4.3$ % echo !!
                          !!


                        (you won't be able not to record a command where history substitution has been disabled though. Also note that the default 3rd character of $histchars is # so that history expansion is not done in comments. If you change it, and you enter comments at the prompt, you should be aware of the fact that ! sequences may be expanded there).






                        share|improve this answer

























                          3












                          3








                          3







                          To be able to disable history expansion on a particular command line, you can use space as the 3rd character of $histchars:



                          histchars='!^ '


                          Then, if you enter your command with a leading space, history expansion will not be performed.



                          bash-4.3$ echo "#!/bin/bash"
                          bash: !/bin/bash: event not found
                          bash-4.3$ echo "#!/bin/bash"
                          #!/bin/bash


                          Note however that leading spaces are also used when $HISTCONTROL contains ignorespace as a way to tell bash not to record a command line in the history.



                          If you want both features indenpendantly, you'll need to choose another character as the 3rd character of $histchars. You want one that doesn't affect the way your command is interpreted. A few options:



                          • using backslash (): echo foo works but that has the side effect of disabling aliases or keywords.

                          • TAB: to insert it in first position, you need to press Ctrl+VTab though.


                          • if you don't mind typing two keys, you can pick any character that normally doesn't appear in first position (%, @, ?, pick your own) and make an empty alias for it:



                            histchars='!^%'
                            alias %=


                            Then enter that character, space and your command:



                            bash-4.3$ % echo !!
                            !!


                          (you won't be able not to record a command where history substitution has been disabled though. Also note that the default 3rd character of $histchars is # so that history expansion is not done in comments. If you change it, and you enter comments at the prompt, you should be aware of the fact that ! sequences may be expanded there).






                          share|improve this answer













                          To be able to disable history expansion on a particular command line, you can use space as the 3rd character of $histchars:



                          histchars='!^ '


                          Then, if you enter your command with a leading space, history expansion will not be performed.



                          bash-4.3$ echo "#!/bin/bash"
                          bash: !/bin/bash: event not found
                          bash-4.3$ echo "#!/bin/bash"
                          #!/bin/bash


                          Note however that leading spaces are also used when $HISTCONTROL contains ignorespace as a way to tell bash not to record a command line in the history.



                          If you want both features indenpendantly, you'll need to choose another character as the 3rd character of $histchars. You want one that doesn't affect the way your command is interpreted. A few options:



                          • using backslash (): echo foo works but that has the side effect of disabling aliases or keywords.

                          • TAB: to insert it in first position, you need to press Ctrl+VTab though.


                          • if you don't mind typing two keys, you can pick any character that normally doesn't appear in first position (%, @, ?, pick your own) and make an empty alias for it:



                            histchars='!^%'
                            alias %=


                            Then enter that character, space and your command:



                            bash-4.3$ % echo !!
                            !!


                          (you won't be able not to record a command where history substitution has been disabled though. Also note that the default 3rd character of $histchars is # so that history expansion is not done in comments. If you change it, and you enter comments at the prompt, you should be aware of the fact that ! sequences may be expanded there).







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Aug 21 '14 at 21:17









                          Stéphane ChazelasStéphane Chazelas

                          301k55564916




                          301k55564916





















                              2














                              The proposed solutions don't work in, e.g., the following example:



                              $ bash -c "echo 'hello World!'"
                              -bash: !'": event not found
                              $


                              In this case the bang can be printed using its octal ASCII code:



                              $ bash -c "echo -e 'hello World041'"
                              hello World!
                              $





                              share|improve this answer




















                              • 1





                                In your first command, the ! is between double quotes, where as other answers indicate it retains its history expansion meaning. You could use bash -c 'echo '''hello World!'' or bash -c "echo 'hello World"!"'".

                                – Gilles
                                Dec 26 '10 at 11:18











                              • Without the -i parameter, the environment may be altered (like your ~/.profile doesn't run). However, doing -i means any output from your .profile will be captured in the output of the command you actually want to run.

                                – Brent
                                Apr 6 '15 at 21:18















                              2














                              The proposed solutions don't work in, e.g., the following example:



                              $ bash -c "echo 'hello World!'"
                              -bash: !'": event not found
                              $


                              In this case the bang can be printed using its octal ASCII code:



                              $ bash -c "echo -e 'hello World041'"
                              hello World!
                              $





                              share|improve this answer




















                              • 1





                                In your first command, the ! is between double quotes, where as other answers indicate it retains its history expansion meaning. You could use bash -c 'echo '''hello World!'' or bash -c "echo 'hello World"!"'".

                                – Gilles
                                Dec 26 '10 at 11:18











                              • Without the -i parameter, the environment may be altered (like your ~/.profile doesn't run). However, doing -i means any output from your .profile will be captured in the output of the command you actually want to run.

                                – Brent
                                Apr 6 '15 at 21:18













                              2












                              2








                              2







                              The proposed solutions don't work in, e.g., the following example:



                              $ bash -c "echo 'hello World!'"
                              -bash: !'": event not found
                              $


                              In this case the bang can be printed using its octal ASCII code:



                              $ bash -c "echo -e 'hello World041'"
                              hello World!
                              $





                              share|improve this answer















                              The proposed solutions don't work in, e.g., the following example:



                              $ bash -c "echo 'hello World!'"
                              -bash: !'": event not found
                              $


                              In this case the bang can be printed using its octal ASCII code:



                              $ bash -c "echo -e 'hello World041'"
                              hello World!
                              $






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Feb 4 '12 at 20:29









                              Kevin

                              27.1k106299




                              27.1k106299










                              answered Dec 24 '10 at 17:40









                              AttiAtti

                              291




                              291







                              • 1





                                In your first command, the ! is between double quotes, where as other answers indicate it retains its history expansion meaning. You could use bash -c 'echo '''hello World!'' or bash -c "echo 'hello World"!"'".

                                – Gilles
                                Dec 26 '10 at 11:18











                              • Without the -i parameter, the environment may be altered (like your ~/.profile doesn't run). However, doing -i means any output from your .profile will be captured in the output of the command you actually want to run.

                                – Brent
                                Apr 6 '15 at 21:18












                              • 1





                                In your first command, the ! is between double quotes, where as other answers indicate it retains its history expansion meaning. You could use bash -c 'echo '''hello World!'' or bash -c "echo 'hello World"!"'".

                                – Gilles
                                Dec 26 '10 at 11:18











                              • Without the -i parameter, the environment may be altered (like your ~/.profile doesn't run). However, doing -i means any output from your .profile will be captured in the output of the command you actually want to run.

                                – Brent
                                Apr 6 '15 at 21:18







                              1




                              1





                              In your first command, the ! is between double quotes, where as other answers indicate it retains its history expansion meaning. You could use bash -c 'echo '''hello World!'' or bash -c "echo 'hello World"!"'".

                              – Gilles
                              Dec 26 '10 at 11:18





                              In your first command, the ! is between double quotes, where as other answers indicate it retains its history expansion meaning. You could use bash -c 'echo '''hello World!'' or bash -c "echo 'hello World"!"'".

                              – Gilles
                              Dec 26 '10 at 11:18













                              Without the -i parameter, the environment may be altered (like your ~/.profile doesn't run). However, doing -i means any output from your .profile will be captured in the output of the command you actually want to run.

                              – Brent
                              Apr 6 '15 at 21:18





                              Without the -i parameter, the environment may be altered (like your ~/.profile doesn't run). However, doing -i means any output from your .profile will be captured in the output of the command you actually want to run.

                              – Brent
                              Apr 6 '15 at 21:18











                              -1














                              Ever since Bash 4.3, you can now use double quotes to quote the history expansion character:



                              $ bash --version
                              GNU bash, version 4.3...
                              [...]
                              $ echo "Hello World!"
                              Hello World!





                              share|improve this answer


















                              • 4





                                No, that's only ! followed by " that is no longer special. echo "foo!" is OK, echo "foo!bar" is not

                                – Stéphane Chazelas
                                Sep 16 '16 at 13:07















                              -1














                              Ever since Bash 4.3, you can now use double quotes to quote the history expansion character:



                              $ bash --version
                              GNU bash, version 4.3...
                              [...]
                              $ echo "Hello World!"
                              Hello World!





                              share|improve this answer


















                              • 4





                                No, that's only ! followed by " that is no longer special. echo "foo!" is OK, echo "foo!bar" is not

                                – Stéphane Chazelas
                                Sep 16 '16 at 13:07













                              -1












                              -1








                              -1







                              Ever since Bash 4.3, you can now use double quotes to quote the history expansion character:



                              $ bash --version
                              GNU bash, version 4.3...
                              [...]
                              $ echo "Hello World!"
                              Hello World!





                              share|improve this answer













                              Ever since Bash 4.3, you can now use double quotes to quote the history expansion character:



                              $ bash --version
                              GNU bash, version 4.3...
                              [...]
                              $ echo "Hello World!"
                              Hello World!






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Sep 16 '16 at 12:18









                              FlimmFlimm

                              1,37541828




                              1,37541828







                              • 4





                                No, that's only ! followed by " that is no longer special. echo "foo!" is OK, echo "foo!bar" is not

                                – Stéphane Chazelas
                                Sep 16 '16 at 13:07












                              • 4





                                No, that's only ! followed by " that is no longer special. echo "foo!" is OK, echo "foo!bar" is not

                                – Stéphane Chazelas
                                Sep 16 '16 at 13:07







                              4




                              4





                              No, that's only ! followed by " that is no longer special. echo "foo!" is OK, echo "foo!bar" is not

                              – Stéphane Chazelas
                              Sep 16 '16 at 13:07





                              No, that's only ! followed by " that is no longer special. echo "foo!" is OK, echo "foo!bar" is not

                              – Stéphane Chazelas
                              Sep 16 '16 at 13:07

















                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Unix & Linux Stack Exchange!


                              • 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%2funix.stackexchange.com%2fquestions%2f3051%2fhow-to-echo-a-bang%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 check contact read email or not when send email to Individual?

                              Displaying single band from multi-band raster using QGIS

                              How many registers does an x86_64 CPU actually have?