Linux怎么实现自动填充内容到命令提示符?

~/.bashrc 中添加了函数 rhist,其中的 $result 是由历史命令查询工具 rhist 返回的。
现在是 $result 添加到了历史记录中了,但要手动用上方向键调出来。我想的是把 $result 自动填充到命令提示符上去,等待编辑或者执行。

rhist() {
    local result=$(command rhist "$@")
    [ -n "$result" ] && echo "$result" && echo "$result" >> ~/.bash_history && history -r
}

现在是:
[root@VM-4-8-centos ~]# rhist
结果内容
[root@VM-4-8-centos ~]#

期望的结果是:
[root@VM-4-8-centos ~]# rhist
[root@VM-4-8-centos ~]# 结果内容

AI工具查过,没有得到想要的结果

阅读 894
1 个回答

使用 read -e -i 实现命令行预填充

rhist() {
    local result=$(command rhist "$@")
    if [ -n "$result" ]; then
        echo "$result"
        echo "$result" >> ~/.bash_history
        history -r
        read -e -i "$result" -p "$USER@$HOSTNAME:\$ " cmd
        eval "$cmd"
    fi
}
推荐问题