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

后端 未结 4 544
心在旅途
心在旅途 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:52

    In your last example,

    echo "$(echo '!b')"
    

    the exclamation point is not single-quoted. Because history expansion occurs so early in the parsing process, the single quotes are just part of the double-quoted string; the parser hasn't recognized the command substitution yet to establish a new context where the single quotes would be quoting operators.

    To fix, you'll have to temporarily turn off history expansion:

    set +H
    echo "$(echo '!b')"
    set -H
    

提交回复
热议问题