我该如何逃脱!所以终端看到 echo "Hello World!" 的结束 dquote

How do I escape an ! so terminal sees a closing dquote for echo "Hello World!"

问题:

通过在终端中输入 echo "Hello World!",我得到的答复是:dquote>

问题(2 部分):

如果我使用单引号,终端 returns 我期望看到的结果:Hello World!

  1. 如何逃脱!
  2. 这里为什么双引号和单引号不同?

我试过了:

用反斜杠,echo "Hello World/!"好像不行,我还是被退回了dquote>。 我还尝试寻找有关转义字符和 Whosebug 答案的其他答案。他们通常最终会超越我的头脑。

关于问题 1:(如何转义 !?)

您可以使用"'!'"

示例:

echo "Hello World"'!'""'!'" I'm there."

结果:

Hello World!! I'm there.

关于问题2:(这里为什么双引号和单引号不同?

因为!是默认的"history expansion"字符,里面的双引号"history expansion"被执行。

Bash 手册:

3.1.2.2 Single Quotes

Enclosing characters in single quotes (') preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash.

3.1.2.3 Double Quotes

Enclosing characters in double quotes (") preserves the literal value of all characters within the quotes, with the exception of $, `, \, and, when history expansion is enabled, !. The characters $ and ` retain their special meaning within double quotes (see Shell Expansions). The backslash retains its special meaning only when followed by one of the following characters: $, `, ", \, or newline. Within double quotes, backslashes that are followed by one of these characters are removed. Backslashes preceding characters without a special meaning are left unmodified. A double quote may be quoted within double quotes by preceding it with a backslash. If enabled, history expansion will be performed unless an ! appearing in double quotes is escaped using a backslash. The backslash preceding the ! is not removed.

The special parameters * and @ have special meaning when in double quotes (see Shell Parameter Expansion).