echo “#!” fails — “event not found”

前端 未结 5 856
长情又很酷
长情又很酷 2020-11-22 08:30

The following fails and I don\'t understand why:

$ echo \"#!\"

the following also fails with the same error message:

$ echo         


        
5条回答
  •  不知归路
    2020-11-22 08:55

    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

提交回复
热议问题