Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

4 Answers 4

up vote 20 down vote accepted

! 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.

share|improve this answer
    
Yes, but should happen if I want to use echo -e "some text $ENV_VAL some more text\nReboot now!" ? –  Maxim Veksler Dec 2 '10 at 13:25
1  
Write two echo statements; you get the linefeed for free. –  MadHatter Dec 2 '10 at 14:22
add comment

You can turn off history substitution using set +H.

share|improve this answer
add comment

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.

share|improve this answer
1  
!! to fill in the complete last command entered is remarkably useful; particularly, sudo !! or pfexec !! –  jgoldschrafe Dec 2 '10 at 11:20
    
I also find sudo and pfexec to be nice utilities, but no need to be so enthusiastic. –  LeartS Apr 17 at 0:08
add comment

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|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.