在哪里可以找到有关“ sudo !!”命令的文档?


Answers:



29

实际上是sudo !!,它由sudo您可能熟悉的命令和事件指示符组成,该事件指示符!!指的是最后输入的命令。您可以bash在该Event Designators部分下的手册页中找到更多信息。

Event Designators
   An event designator is a reference to a command line entry in the  his‐
   tory  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.
   !!     Refer to the previous command.  This is a synonym for `!-1'.
   !string
          Refer to the most recent command preceding the current  position
          in the history list starting with string.
   !?string[?]
          Refer to the most recent command preceding the current postition
          in the history list containing string.  The trailing  ?  may  be
          omitted if string is followed immediately by a newline.
   ^string1^string2^
          Quick  substitution.   Repeat  the  previous  command, replacing
          string1 with string2.  Equivalent  to  ``!!:s/string1/string2/''
          (see Modifiers below).
   !#     The entire command line typed so far.

3

功能的分离是最精美的设计原则之一,这使得Linux / Unix比其他替代方案(其中每个程序都是独立的约定和功能的独立岛)的功能强大得多。

“让每个程序都做一件事情,并且做好事”

而不是实施!在sudo(或其他任何命令)内部,可以从重复执行上一个命令中受益-一次执行(在shell中),所有命令都可以从中受益。因此,您可以执行以下操作:

$ echo !!     # will echo the last command
$ time !!     # will repeat and time the last command
$ strace !!   # will repeat the last program while system-call tracing it

等等。

但这并没有到此结束。外壳程序不仅仅可以通过!扩展历史记录。事件指示符。在执行命令之前,它将进行变量扩展,文件名通配符扩展(globbing),命令替换,文件/ IO重定向等。所有这些都可以在从Shell调用的任何命令中使用和使用。

另一个很大的好处是,如果您花一些时间来学习外壳程序(在本例中为“ man bash”),则需要学习一次,并且可以随时随地使用这些强大的功能。学习一组强大的原理和约定要容易得多,而不是重新学习每个程序或实用程序中如何处理命令行agr。

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.