是不是比较好用!!还是历史?

是不是比较好用!!还是历史?

我发现了一个有用的快捷方式,当您忘记命令之前!!可以使用它。sudo您将轻松输入

$ sudo !!

很长一段时间以来,我一直在用另一种方式做这件事up arrowctrl + home然后输入sudo。优点是,您可以看到命令。

您会建议改变习惯并使用宁可吗!!?为什么?

答案1

如果你只是输入它并且仍然看到它,那么我不明白为什么不使用,!!但当然不要盲目地这样做,最终运行之前的操作,这可能会把事情搞砸。

我无法想象在很多情况下以这种方式重做最后一个命令会很危险,除非你抽的东西真的很烈。

答案2

正如您和朱莉所注意到的,这两种方式都有其优点和缺点。但真正最好的解决方案是在需要时同时使用它们。所以,当你不确定!!会发生什么时,

  • 首先UpArrow看看它是什么
  • 然后DownArrow
  • 进而sudo !!

关于历史,还有更多的技巧。这是来自man bash.

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  position
          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.

查看整个故事历史扩展,转到man bash并在那里找到该部分历史扩展

相关内容