What is "-bash: !": event not found"

251,922

Solution 1

You can turn off history substitution using set +H.

Solution 2

! is a special character to bash, it is used to refer to previous commands; eg,

!rm

will recall and execute the last command that began with the string "rm", and

!rm:p

will recall but not execute the last command that began with the string "rm". bash is interpreting the exclamation mark in echo "reboot your instance!" as "substitute here the last command that began with the character(s) immediately following the exclamation mark", and grumbles at you that it cannot find an event (command) in your history that began with a single double-quote.

Try

echo reboot your instance\!

to protect (escape) the exclamation mark from bash.

Solution 3

To solve your original problem, try using single quotes, rather than double quotes. With the latter, bash will attempt to expand certain characters before passing the result on to the command (echo in this case). With single quotes, bash passes the entire string, unchanged.

! is used in commands to refer to the command line history. See: http://tldp.org/LDP/abs/html/abs-guide.html#HISTCOMMANDS for a full set. With the above example, bash is trying to expand !" as a reference to an event before echo gets a look in, hence the error.

Note that in scripts, all of the history commands are disabled, as they only make sense in an interactive shell.

The only one I use on a regular basis, is !$. It expands to the last argument of the previous command. It's a useful shorthand in places.

Solution 4

Just put a space between ! and " than it'll work.

Solution 5

Yes, ! is a special character to bash, it is used to refer to previous commands.

Few of the ways you can handle the situation

The following will output the string as it is

echo 'Reboot your instance!'

The following will execute the command and concatenate the string

echo 'escaping #'" adding `which python` to the string"
echo '#!'`which python`
Share:
251,922

Related videos on Youtube

Maxim Veksler
Author by

Maxim Veksler

Doing healthy things at K Health

Updated on September 17, 2022

Comments

  • Maxim Veksler
    Maxim Veksler over 1 year

    Try executing the following under a bash shell echo "Reboot your instance!"

    On my installation:

    root@domU-12-31-39-04-11-83:/usr/local/bin# bash --version
    GNU bash, version 4.1.5(1)-release (i686-pc-linux-gnu)
    Copyright (C) 2009 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
    
    This is free software; you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.
    root@domU-12-31-39-04-11-83:/usr/local/bin# uname -a
    Linux domU-12-31-39-04-11-83 2.6.35-22-virtual #35-Ubuntu SMP Sat Oct 16 23:57:40 UTC 2010 i686 GNU/Linux
    root@domU-12-31-39-04-11-83:/usr/local/bin# echo "Reboot your instance!"
    -bash: !": event not found
    

    Can anyone please explain what is "bash events?" I've never heard this concept before. Also, how should I output "!" at the end of the sentence?

  • jgoldschrafe
    jgoldschrafe over 13 years
    !! to fill in the complete last command entered is remarkably useful; particularly, sudo !! or pfexec !!
  • Maxim Veksler
    Maxim Veksler over 13 years
    Yes, but should happen if I want to use echo -e "some text $ENV_VAL some more text\nReboot now!" ?
  • MadHatter
    MadHatter over 13 years
    Write two echo statements; you get the linefeed for free.
  • LeartS
    LeartS about 10 years
    I also find sudo and pfexec to be nice utilities, but no need to be so enthusiastic.
  • Icebreaker
    Icebreaker over 9 years
    Could you please add an explanation as to why your solution works?
  • mmey
    mmey over 9 years
    This works because Bash only treats ! as a special character if it's directly followed by a non-whitespace character. However, it will also add an extra blank to your output, so that might not always be desired...
  • mmey
    mmey over 9 years
    Simple solution is to use single quotes and double quotes at once: echo -e "Text\nReeboot"'!'
  • MadHatter
    MadHatter over 9 years
    I'm not sure that's simpler, but it will work, yes.
  • Hafizur Rahman
    Hafizur Rahman over 9 years
    This sucks. If I don't escape the exclamation mark, bash throws a fit. If I do escape it, the backslash is included in the final string. Why do you do this, Bash!! (pastebin.com/g4PYv56A)
  • MadHatter
    MadHatter over 9 years
    @Hubro in fairness, I'm not sure that's all bash's fault. You have somewhat complicated matters by using ruby to feed things to bash, and it's not clear to me what part ruby is playing in the stripping or reinforcing of escape characters.
  • Hafizur Rahman
    Hafizur Rahman over 9 years
    @MadHatter It ain't Ruby's fault: pastebin.com/6ezCP1HS
  • MadHatter
    MadHatter over 9 years
    You may be right; I don't know enough ruby to understand the intricacies of the <<< operator. All I do know is that when bash is used unmediated by anything else, the problem admits of a solution; see above.
  • Mukul Goel
    Mukul Goel about 9 years
    @MadHatter : what to do if ! is part of a password that I am trying to enter and getting this error?
  • Mukul Goel
    Mukul Goel about 9 years
    @MadHatter Okkie. I will do so.
  • Gavin Ward
    Gavin Ward almost 8 years
    Do you know the reasoning behind the syntax? It seems really counter intuitative. You'd think you'd type +H to enable something and -H to disable something.
  • Dennis Williamson
    Dennis Williamson almost 8 years
    @PunkyGuy: I don't know the history (pun intended) behind it, but Unix options typically start with a hyphen (or minus) and + is simply the opposite of that.
  • ΤΖΩΤΖΙΟΥ
    ΤΖΩΤΖΙΟΥ almost 8 years
    $_ works in more shells than bash and it's a proper variable, unlike !$. (and @LeartS: I liked your joke's lack of emoticons. ;) )
  • MadHatter
    MadHatter over 7 years
    @MarkusZeller thanks for letting me know; I'm glad this answer is still helping people, six years on!
  • Bodo Thiesen
    Bodo Thiesen about 6 years
    Probably a bug, because here, I have 4.4.12 and yet again, I need set +H or quoting. Or you have set +H somewhere (like in /etc/profile).
  • JohnnyD92
    JohnnyD92 almost 6 years
    Using !-char not at the end of the sentence verifies bash's event detection is still working fine. Version is 4.3.30 ........ $ echo "Recheck your sentence!foo" bash: !foo: event not found
  • leetbacoon
    leetbacoon over 3 years
    echo 'Reboot your instance!' works as well, as characters in between ' makes the shell interpret them literally.
  • Dario Seidl
    Dario Seidl over 3 years
    From help set (in bash): "Using + rather than - causes these flags to be turned off."
  • Andrew Schulman
    Andrew Schulman over 3 years
    The question was about bash.
  • Gabriel Staples
    Gabriel Staples over 3 years
    After reading this answer, I learned what I needed, and in this other answer of mine here I demonstrate 2 different ways to escape the ! char.
  • Pavel Šimerda
    Pavel Šimerda about 3 years
    @leetbacoon As long as you don't need to expand variables in the error string.
  • leetbacoon
    leetbacoon almost 3 years
    @PavelŠimerda Right, however if a variable needs expanding, just end the single quotes and begin your variable, e.g. echo 'Reboot '"${machine_name}"' now!'
  • Jasen
    Jasen almost 3 years
    @Hubro ruby <<< "puts 'Hello, world"'!'"'" ruby <<< 'puts "Hello, world!"'
  • Robin Khurana
    Robin Khurana about 2 years
    @MadHatter: how does ruby enter into this at all? We're just talking about bash and bash's built-in echo command here. :-o
  • Robin Khurana
    Robin Khurana about 2 years
    While this question correctly addresses bash, since that's what the question asked, I thought I'd mention that in zsh, the approximate equivalent is set -K or setopt NO_BANG_HIST (capitalization and underscores optional, but that's how it shows up in the docs, so referencing it that way in case folks want further details).
  • MadHatter
    MadHatter about 2 years
    @lindes see comment 5 above, by Hubro, who brought it up.