I want the shell to detect that I had run a specific command and then after running the command, run another command.

For Example : When every I run the command : git commit -m " "

First finish the above command and then run another command such as : python check.py

I'm inclined towards modifying the .bash_rc file. Am I right ?

Thanks in advance.

  • 1
    Git has hooks for this purpose. scotch.io/tutorials/… – slm Aug 11 '18 at 14:53
  • I've deleted my answer as I've misunderstood the question - apologies for the confusion. – Seamus Aug 11 '18 at 16:29
  • slm , yes git hooks is the answer to my problem. Thank you so much..!!! – 100rabh Aug 11 '18 at 17:29

Use the 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.


  1. 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'"
        return 1
        return 0

shopt -s extdebug
trap "myfunc" DEBUG

echo 1
echo 2

Executing this script:

$ bash test.sh
runing after 'echo 1'
  • Hi Ralf , thanks for the reply. But even if I write the trap command in .bash_rc it will run in every terminal ,where as I needed it to run only in a particular git repo and not other git repos. Any other way around this ? I know that git hooks can be used . Just wanted to know how to use unix commands to do this. – 100rabh Aug 11 '18 at 17:48
  • In the test function, you can also check $PWD or anything else. – RalfFriedl Aug 11 '18 at 17:49
  • Wow I guess that'll do it. Thanks for the help...!! – 100rabh Aug 11 '18 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.