The following fails and I don\'t understand why:
$ echo \"#!\"
the following also fails with the same error message:
$ echo
By default, bash supports csh compatible history-expansion.
In bash
echo #!
will only print a newline, as # starts a comment.
In
echo "#!"
the # is part of the string started with ". Such strings are still inspected by bash for special characters. ! is a special character iff it is followed by any other text.
-bash: !": event not found
In this case, bash expects the !" token to refer to a previous command in shell history starting with ", and does not find one. All by itself, ! will not trigger this behaviour:
$ echo \# !
# !
$ echo fee ! fie
fee ! fie
Finally,
$ echo !echo
produces two lines, the first line is printed by the shell to show how the pattern above expands to:
echo echo '# !'
while the second line is just the result of executing the expanded command: echo # !
See Also: The Bash man page on History Expansion