trap command in bash.
trap [-lp] [[arg] sigspec ...]
If a sigspec is DEBUG, the command arg is executed before every simple command ...
Now your only problem is that your trap command is run before and not after the command. But you can refer the the command to be executed with
$BASH_COMMAND, and you can cause the command not to be executed.
- If the command run by the DEBUG trap returns a non-zero value, the next command is skipped and not executed.
So set a DEBUG trap, if you detect the command you are interested in, execute
$BASH_COMMAND, execute your own command and then cause the original command not to be run.
Try this example:
function myfunc ()
if test "$BASH_COMMAND" = "echo 1"; then
echo "runing after 'echo 1'"
shopt -s extdebug
trap "myfunc" DEBUG
Executing this script:
$ bash test.sh
runing after 'echo 1'