AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1326342
Accepted
Kevin Berry
Kevin Berry
Asked: 2021-03-27 10:03:52 +0800 CST2021-03-27 10:03:52 +0800 CST 2021-03-27 10:03:52 +0800 CST

如何在不执行的情况下按编号从历史记录中调用命令?

  • 772

如何在不执行的情况下按数字从历史记录中获取命令到我的命令行?

This immediately executes command number 555 which I'm not looking for:
$ history 10
$ !555

This opens the command up in an editor, which is overkill most of the time:
$ history 10
$ fc 555

This is an example of what I'm looking for:
$ history 10
$ #555
$ [command 555 from history listing now sitting here on my command line ready to edit or execute]

谢谢!

command-line
  • 5 5 个回答
  • 2099 Views

5 个回答

  • Voted
  1. Best Answer
    user986805
    2021-03-27T10:59:42+08:002021-03-27T10:59:42+08:00
    shopt -s histverify
    

    如果启用了 histverify shell 选项,并且正在使用 Readline,则历史替换不会立即传递给 shell 解析器。相反,扩展的行被重新加载到 Readline 编辑缓冲区中以进行进一步修改。

    • 15
  2. Rinzwind
    2021-03-27T10:08:12+08:002021-03-27T10:08:12+08:00

    :p在数字后面加上。

    例子:

    1357  locate pam_loginuid
    1358  history
    rinzwind@discworld:~$ !1358:p
    history
    rinzwind@discworld:~$ !1357:p
    locate pam_loginuid
    rinzwind@discworld:~$ 
    

    打印以显示而不执行。

    要在 BASH 中使用它,您需要做更多的事情。示例:
    https ://tldp.org/LDP/abs/html/histcommands.html

    #!/bin/bash
    set -o history
    var=$(history); echo "$var"   # 1  var=$(history)`
    

    会将所有历史记录放入 var 中,您需要更多逻辑来找到所需的命令。

    • 11
  3. Ravexina
    2021-03-29T06:24:26+08:002021-03-29T06:24:26+08:00

    如果您正在使用bash,我想您是;输入历史编号:

    $ !555
    

    然后按:Ctrl+ Alt+ e,现在历史记录中所需的命令正在您的提示符中等待执行,而不会更改历史记录的默认行为:

    $ command
    

    它也适用于aliases,substitutions和expansions历史论据说:$ echo !555:1, ~, $HOME, $(echo hi).


    这就是我如何确保在实际运行之前确切执行历史记录中的哪些命令。

    • 7
  4. Olivier Dulac
    2021-03-30T02:05:45+08:002021-03-30T02:05:45+08:00

    编辑:我一开始错误地说“ctrl-f”实际上是“ctrl-r”......(我每天使用多次)。我现在编辑了正确的命令...

    不是您要求的方法,但可能适合您似乎需要做的事情(即,在历史记录中找到上一行并在再次输入/执行之前对其进行编辑):

    # in bash
    [Ctrl]+r  Foo 
    # This recalls the previous history line containing Foo, in edit mode.
    # You can then:
    # [Enter] to execute it "as is", and returns to prompt.
    #  -or- [Ctrl]+o executes it "as is" + presents the following
    #       history line, ready to be executed
    #  -or- <Leftarrow> or <Rightarrow> makes that line the current one
    #         and you can then edit it before executing it (what OP asked for)
    #  -or- [Ctrl]+c to return to the prompt without executing 
    #        the currently displayed command line.
    #  -or- another [Ctrl]+r search the previous occurrence of Foo
       
    

    这样就无需进行历史记录并找出您需要编辑的历史编号以及实际检索和编辑它的另一个组合: [Ctrl]+r Something将为您找到包含Something 的前一行并在编辑中显示它模式。一般来说,它要快得多。

    如果您经常需要编辑 1 个命令并在其后跟随一系列重复的命令,则 Ctrl-o(而不是 enter)特别有用

    还有一个不错的奖励:这个 ctrl-r 等:通常在其他程序中可用(例如:mariadb-server cli [用于检索最后一个“使用”命令],可能还有许多其他程序)。它可能基于(可能由或至少使用)“readline”机制,但我不知道细节......

    • 2
  5. Raffa
    2021-03-27T10:41:44+08:002021-03-27T10:41:44+08:00
    • 您可以像这样直接使用 bash 内置命令:read

      read -e -i "!555" -p "${PS1@P}" input; $input
      
    • 或者你可以将它添加到你喜欢的函数中:~/.bashrc

      showhist() {
      
              read -e -i "$*" -p "${PS1@P}" input
              $input
      
      }
      

      并像这样使用它:

      showhist !555
      

    这将在提示符处显示命令,您可以对其进行编辑....Enter执行或Ctrl+c中止。

    • 1

相关问题

  • 如何从命令行仅安装安全更新?关于如何管理更新的一些提示

  • 如何从命令行刻录双层 dvd iso

  • 如何从命令行判断机器是否需要重新启动?

  • 文件权限如何工作?文件权限用户和组

  • 如何在 Vim 中启用全彩支持?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve