终端更换!后跟一个带有命令的数字

usb*_*102 12 command-line bash history bash-history

我对我的 ubuntu 终端会话感到非常困惑,它似乎!用看似随机的命令替换了命令中以数字开头的部分。它通过!87将其替换为screen -l!88并替换为ls.

任何想法为什么会发生这种情况将不胜感激。

des*_*ert 20

bash是历史扩展,例如

!87
Run Code Online (Sandbox Code Playgroud)

从历史行重新执行命令87

您可以在man bash“历史扩展”部分中找到此功能的说明:

       An  event  designator  is  a  reference  to a command line entry in the
       history list.  Unless the reference is absolute, events are relative to
       the current position in the history list.

       !      Start  a  history substitution, except when followed by a blank,
              newline, carriage return, = or ( (when the extglob shell  option
              is enabled using the shopt builtin).
       !n     Refer to command line n.
       !-n    Refer to the current command minus n.
Run Code Online (Sandbox Code Playgroud)

所以要快速调用最后一个命令,请执行!-1最后一个命令!-5。一个方便的同义词!-1!!- 如果您调用 egapt install something并忘记了sudo,只需执行即可sudo !!

只有反斜杠 ( \ ) 和单引号可以引用历史扩展字符。

为避免历史扩展,您需要使用反斜杠 ( \!) 或使用单引号 ( '!')对感叹号进行转义。

  • 要将文字 `!` 传递给你的命令并避免历史扩展,你需要单引号或转义它们:`foo '!87'` 或 `foo \!87`。(双引号也会对其内容进行扩展。) (3认同)