Closed Tallicia closed 2 years ago
Is there a way to add a post_cmd array so after the pre_cmds and command is run something additional is run, such as a duration caclulation?
I think the names of the hooks are somehow opposite. I guess what you are calling pre_cmds
is actually what is called preexec
in bash-preexec
, and what you call post_cmd
is usually called postexec
(unsupported by bash-preexec) but is mostly the same as what is called precmd
in bash-preexec
. The difference is that precmd
is also called on the startup and after empty commands, while postexec
is called only after the actual user commands. You can set in preexec
a flag variable telling that the command has been executed, and then you check and clear that flag in precmd
to determine whether it should run postexec
hooks.
For calculating execution duration you can indeed use precmd + preexec (I have an example in prompt.gem, see prompt::_command_start
and prompt::_set_ps1
).
However note that bash-preexec is simply a user-friendly wrapper around the hooks bash exposes, and bash doesn't expose enough hooks to fully implement pre- and post- hooks for all events in an interactive shell. For example #28 discusses using the relatively-new $PS0
hook but the consensus there is it's not powerful enough to be generally useful. Similarly, precmd is not equivalent to "postexec" even though it's usually similar enough to not matter. precmd runs before a new command prompt is printed, which generally happens right after an execution finishes but isn't strictly required (e.g. precmd runs when a new shell opens, even though nothing has executed yet).
Is there a way to add a post_cmd array so after the pre_cmds and command is run something additional is run, such as a duration caclulation?