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


Answers:


20

bash是历史的扩展,例如

!87

从历史记录行重新执行命令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.

因此,要快速调用最后一个命令,请!-1为第五个最后一个命令执行和!-5。一个方便的同义词!-1!!–如果您调用eg apt install something并忘记了sudo,只需执行即可sudo !!,您就很好了。

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

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


3
要将litereal传递!给您的命令并避免历史扩展,您需要单引号或将其转义: foo '!87'foo \!87。(双引号也会对其内容进行扩展。)
quixotic
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.