1

In bash:

$ a=1
$ echo $a
1
$ echo "$a"
1

But:

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

Why in "echo !", bash don't expand.

3
  • 1
    what were you expecting to happen ? the history would be "echo !!" (two exclamation marks)
    – Sirex
    Feb 23, 2011 at 8:33
  • @Sirex Why in echo "!" expand and echo ! not expand?
    – osdyng
    Feb 23, 2011 at 8:34
  • What do you expect it to expand into? As Sirex has said, for the last command, it's "!!", and as ooshro has said, a single "!" indicates to the shell that more will follow to inform the expansion. Unless you give bash another character to say what you're trying to select from the history, it'll fail, as it is indeed failing.
    – MadHatter
    Feb 23, 2011 at 8:58

1 Answer 1

8

In "echo !", after bang followed the carriage return.

man bash

! Start a history substitution, except when followed by a blank, newline, carriage return, = or ( (when the extglob shell option is enabled using the shopt builtin).

2
  • why echo -e "!\n" and echo -e "!\r" expand?
    – osdyng
    Feb 23, 2011 at 8:44
  • @osdyng because after bang followed slash
    – ooshro
    Feb 23, 2011 at 8:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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