How to escape history expansion exclamation mark ! inside a double quoted string?

后端 未结 4 541
心在旅途
心在旅途 2020-11-27 20:27

EDIT: the command substitution is not necessary for the surprising behavior, although it is the most common use case. The same question applies to just echo \"\'!b\'\"

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-27 20:42

    If History Expansion is enabled, you can only echo the ! character if it is put in single quotes, escaped or if followed by a whitespace character, carriage return, or =.

    From man bash:

       Only backslash (\) and single quotes can  quote  the  history
       expansion character.
    
       Several  characters inhibit history expansion if found immediately fol-
       lowing the history expansion character, even if it is unquoted:  space,
       tab,  newline,  carriage return, and =.
    

    I believe the key word here is “Only”. The examples provided in the question only consider the outer most quotes being double quotes.

提交回复
热议问题