Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a list of scripts

  • ./myscript <param> | grep "asd"

  • ./myotherscript <param> <param> > file

  • ...

How can I automaticaly run another script when one of these command in the list is executed and finished?

share|improve this question
It's not very clear to me what you're trying to do. What/who runs these commands you have? Another script? – Mat May 19 '12 at 12:12
user run a script from command line – Wolfy May 19 '12 at 12:44
Oh. Why don't you put that command at the end of your scripts? – Mat May 19 '12 at 13:12
Because I don't want this... I want to create a list of script/commands wich will triger the execution of this script... is this possible? – Wolfy May 19 '12 at 13:25
Do you mean that you want to run both scripts, and run a third script when the first of the two finishes? Or run a third script twice, when the first script finishes and then when the second script finishes? Or do you mean to run one of these two scripts, and you want to say “and after than run yet another script”? Your question is still very unclear. – Gilles May 19 '12 at 13:28
up vote 3 down vote accepted

There are multiple ways to automatically execute something after a specific command:


Create a function named after your specific command and execute the specific command afterwards. This is in my opinion the simplest and cleanest solution.

function myotherscript() {
   command myotherscript "$@"


With zsh you can create a precmd function which will be executed before the prompt. This allows you to execute arbitrary other commands but you'll have to determine the executed command yourself.

function precmd() {
   last_cmd=$(history -1 |  sed -e "s/^[ ]*[0-9]*[ ]*//g")
   case "$last_cmd" in
   *myscript*) other_command;;
   *myotherscript*) other_other_command;;


Bash has the PROMPT_COMMAND variable allowing you to implement your own precmd function. To use it you have to set it to a command which will be executed before the prompt:


function bash_precmd() {
  last_command=$(history 1 | sed -e "s/^[ ]*[0-9]*[ ]*//g")
  case "$last_command"  in
  *pattern*) command ;;

If you are using bash and want to use something like preexec or precmd have a look at precmd and preexec with bash

share|improve this answer
your "bash" solution worked for me... but I replaced "${PROMPT_COMMAND};bash_precmd" with "bash_precmd"... Thanks! – Wolfy May 20 '12 at 16:46

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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