在任何终端命令之前运行命令

在任何终端命令之前运行命令

有没有一种方法可以在执行任何终端命令之前运行一个命令?

例如:

> ls -ltr
> "Hello you ran ls -ltr" //this is what I would like to achieve
> ..output of ls -ltr will be here

是否可以运行确保在执行任何命令之前运行回显?

谢谢

答案1

您可能想考虑设置一个DEBUG陷阱,它允许您以类似于 的方式设置有效的预执行挂钩zsh。看https://superuser.com/questions/175799/does-bash-have-a-hook-that-is-run-before-executing-a-command

答案2

Bash 有将函数分配给 ps1 的概念,所以我的看起来像

export PROMPT_COMMAND='PS1=$(make_ps1); set_xterm_title'

其中 make_ps1 是

    make_ps1()
    {
    if [ $? = 0 ];then
        echo '\[\e[${host_color}m\][\D{%F %T} \u@\h \W]\[\e[0m\]\n\$ '
    else
        echo '\[\e[7m\e[${host_color}m\][\D{%F %T} \u@\h \W]\[\e[0m\]\n\$ '
    fi
    }

您应该能够利用它来做任何您想做的事情,但它会在执行命令后运行,因此这可能无法满足您的需要。

相关内容