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

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

    This was repeatedly reported as a bug, most recently against bash 4.3 in 2014, for behavior going back to bash 3.

    There was some discussion whether this constituted a bug or expected but perhaps undesirable behavior; it seems the consensus has been that, however you want to characterize the behavior, it shouldn't be allowed to continue.

    It's fixed in bash 4.4, echo "$(echo '!b')" doesn't expand, echo "'!b'" does, which I regard as proper behavior because the single quotes are shell syntax markers in the first example and not in the second.

提交回复
热议问题