使用向上箭头运行上一个命令

pro*_*eek 12 8.04 bash shortcut-keys

使用向上箭头运行上一个命令需要什么设置?对于 Mac,我可以使用向上箭头重新运行我刚刚运行的命令,但它似乎不适用于我的 bash shell。

我使用 8.04(由于某些编译器版本问题,我无法使用最新的发行版)。

添加

我没有更改任何内容,因为它是在 Mac 上使用 VMWare Fusion 全新安装的。

gei*_*rha 14

确保您确实在使用 bash。一个常见的问题是使用useradd而不是adduser用户和组 (GUI) 应用程序创建新用户。对于前者,默认的 shell 集是/bin/sh. 运行 chshCH安格SH ELL),以确保它的设置/bin/bash


Lek*_*eyn 10

确保您的历史记录已启用。您可以通过运行来检查当前状态:

set -o
Run Code Online (Sandbox Code Playgroud)

输出应包含(注意该history on行):

histexpand      on
history         on
ignoreeof       off
Run Code Online (Sandbox Code Playgroud)

如果未启用此功能,则需要运行set -o history. 要使此更改持久化,您需要将其附加到~/.bashrc

set -o history
Run Code Online (Sandbox Code Playgroud)

如果要运行上一条命令,也可以运行下一条命令:

!!
Run Code Online (Sandbox Code Playgroud)

Bash 手册页

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

如果您使用 Bash,您也可以使用默认快捷方式浏览历史记录:

  • Ctrl+ P: 上一个命令
  • Ctrl+ N: 下一个命令

    用于操作历史的命令 previous-history (Cp) 从历史列表中获取前一个命令,在列表中向后移动。next-history (Cn) 从历史列表中获取下一个命令,在列表中向前移动。