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?
!
is a special character to bash, it is used to refer to previous commands; eg,will recall and execute the last command that began with the string "rm", and
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
to protect (escape) the exclamation mark from bash.
You can turn off history substitution using
set +H
.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.Just put a space between
!
and"
than it'll work.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
The following will execute the command and concatenate the string
You can also add
In
./bash_profile
(or use it as a once-off on the command line)To disable
!
from being a 'special history char', or to set it to something else.Warning; some things may break when you set it for example to
~
and possibly other strings.Look:
You could also use a heredoc:
It's annoying in this case because it's not a one-liner but for longer commands (like writing a bash script), it works great.
In
bash 4.3
it seems you no longer need to use single quotes orset +H
:!
is special character for zsh. It points to previous command.If you need to use it as string character, try
\!
escape it:
echo reboot your instance\!
And if you ran into something when you need to use two Exclamations, use:
ls *[\!'!']*