I am trying to create a bash script in which I want to execute a command x only if command y has been executed n times successfully. Thanks for your help in advance!!! And no question isn't duplicate of

  • Do you want it to retry command y unlimited times until it is successful 3 times or only try 3 times total?
    – jesse_b
    Feb 9, 2020 at 16:22
  • No I don't want it to retry , just 3 times total
    – Madhubala
    Feb 9, 2020 at 16:25

1 Answer 1


Using a loop:



for ((i=1;i<=3;i++)); do
    if command y; then

if ((s==3)); then
    command x

We set the s parameter to 0 to keep track of our successful command y attempts. (not really necessary but I prefer to do it)
The for loop will then run 3 times, running command y each time, if command y is successful it will add 1 to s.
After the loop ends, if s equals 3 it will run command x.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .