bashrc函数,将执行输出结果

时间:2019-03-21 01:53:53

标签: bash

我正在尝试搜索历史记录,以获取最后一个历史记录项并执行我的发现。这是我当前在.bashrc上的函数

function hgl() {
        history | grep  "$1" | grep -v hg | tail -n 1 | awk '{ print "!"$1 }'
}

此函数将打印类似!2345的输出,但是我希望能够运行该输出,因为我得到了历史记录搜索的最后一条命令。

2 个答案:

答案 0 :(得分:0)

我做了以下工作,并且有效:

function hgl() {
        RESULT=`history | grep  "$1" | grep -v hg | tail -n 1 | awk '{$1=""; print $0}'`
        eval $RESULT
}

答案 1 :(得分:0)

获得命令编号后,可以使用record_id来运行它:

fc

如果您不想先对其进行编辑,则可以使用无操作命令,例如$ help fc fc: fc [-e ename] [-lnr] [first] [last] or fc -s [pat=rep] [command] Display or execute commands from the history list. fc is used to list or edit and re-execute commands from the history list. FIRST and LAST can be numbers specifying the range, or FIRST can be a string, which means the most recent command beginning with that string. Options: -e ENAME select which editor to use. Default is FCEDIT, then EDITOR, then vi

类似这样:

: