Run a command before any terminal command

You might want to look into setting a DEBUG trap, which allows you to set up what is effectively a pre-exec hook in a manner similar to zsh. See https://superuser.com/questions/175799/does-bash-have-a-hook-that-is-run-before-executing-a-command.


Bash has the concept of assigning a function to ps1 so mine looks like

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

where make_ps1 is

    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
    }

you should be able to leverage that to do whatever you want, but it will run after the command is executed so this might not work for what you need.

Tags:

Bash