How do I execute a program X times in the terminal?

I read that I have to do a bin/bash txt, but I don't know how to execute this X times.

  • what is it, a script, an application, what does it do? what should happen in case of errors? does it close after it ran? should it be checked if it has closed? – Jacob Vlijm Sep 12 '14 at 15:48
  • its just a click, xdotool click I dont think it can create an error. – user3597989 Sep 12 '14 at 16:00
  • @KasiyA: Not exactly as all those answers are looping forever. Here the OP asks for a fixed number of loops. – Sylvain Pineau Sep 12 '14 at 16:34
  • 4
    Or Blufter's: askubuntu.com/a/508752/158442 - but both of them don't belong there, but here. :D – muru Sep 12 '14 at 16:49
  • 2
    @KasiyA: Ask's answer should be here not in Repeat a command every x interval of time in terminal? – Sylvain Pineau Sep 12 '14 at 16:50

You can use xargs and seq. Generally:

seq nr_of_times_to_repeat | xargs -Iz command

For example:

seq 10 | xargs -Iz xdotool click 1

will execute the xdotool click 1 command for 10 times.

  • 1
    This is probably the most elegant solution, not to mention the simplest. – Seth Sep 12 '14 at 17:01
  • I think this is a really good example for explaining the pipe character. – PascalVKooten Sep 17 '14 at 7:53

Open a Terminal and use the following bash command:

for i in {1..5}; do xdotool click 1; done

With a bit of verbosity and 1s delay:

for i in {1..5}; do echo click \#$i && xdotool click 1 && sleep 1; done
click #1
click #2
click #3
click #4
click #5
  • sintax error with 'do'.. – user3597989 Sep 12 '14 at 16:23
  • @user3597989 It works for me, could you check again? – Sylvain Pineau Sep 12 '14 at 16:30
  • @user3597989 oh I know, please remove the first $. It's just there to indicate a shell prompt like sylvain@sylvain-ThinkPad-T430s:~$. We usually add it to command line answers to differentiate the command from its results. – Sylvain Pineau Sep 12 '14 at 16:32
  • @user3597989: I removed the shell prompt to avoid confusion – Sylvain Pineau Sep 12 '14 at 16:48

This should do:


while [ $x -le 10 ]
  <command to run>
  x=$(( $x + 1 ))

where 10 is the number of times to run the command

if you need to build in a little break:


while [ $x -le 10 ]
  <command to run>
  sleep 1
  x=$(( $x + 1 ))

Copy the script into an empty file, replace <command to run> by your xdotool command, save it as run_xdotool.sh, run it by the command:

sh /path/to/run_xdotool.sh

Alternatively, you can make it executable and simply run it by


Another solution: using xdotool's built in repeat option

Since you mention to use it to do clicks, the easiest might be to use xdotool's own built-in repeat option. The format is:

xdotool click --delay <delay> --repeat <repeats> <button>
(delay in milliseconds between the clicks)

To do 10 mouse clicks (button 1) at a row, one second in between, the command is:

xdotool click --delay 1000 --repeat 10 1

If you have GNU Parallel you can run:

seq 10 | parallel -N0 doit

All new computers have multiple cores, but most programs are serial in nature and will therefore not use the multiple cores. However, many tasks are extremely parallelizeable:

  • Run the same program on many files
  • Run the same program for every line in a file
  • Run the same program for every block in a file

GNU Parallel is a general parallelizer and makes is easy to run jobs in parallel on the same machine or on multiple machines you have ssh access to.

If you have 32 different jobs you want to run on 4 CPUs, a straight forward way to parallelize is to run 8 jobs on each CPU:

Simple scheduling

GNU Parallel instead spawns a new process when one finishes - keeping the CPUs active and thus saving time:

GNU Parallel scheduling


A personal installation does not require root access. It can be done in 10 seconds by doing this:

(wget -O - pi.dk/3 || curl pi.dk/3/ || fetch -o - http://pi.dk/3) | bash

For other installation options see http://git.savannah.gnu.org/cgit/parallel.git/tree/README

Learn more

See more examples: http://www.gnu.org/software/parallel/man.html

Watch the intro videos: https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1

Walk through the tutorial: http://www.gnu.org/software/parallel/parallel_tutorial.html

Sign up for the email list to get support: https://lists.gnu.org/mailman/listinfo/parallel

  • 1
    I love parallel and try to suggest its usage wherever I can. One question though to your specific option: why did you prefer to suggest this form instead of parallel -N0 doit ::: {1..10} – Phillip -Zyan K Lee- Stockmann Feb 16 '17 at 13:07
  • 1
    The {1..10} does not work in (t)csh/fish/ash, and the {1..10} construct is limited by the length of the command line, so this will not work: {1..1000000000} whereas seq 1000000000 will work just fine. But for most situations {1..10} will work as expected, and I often use that, too. – Ole Tange Feb 16 '17 at 15:49

You can use a C style for loop which has the advantage over the brace-expansion version ({1..5}) of being able to use variables to specify the end points. Either version is better than using an external utility (seq).

for ((x = 1; x <= t; x++))
    xdotool click 1

All on one line:

t=5; for ((x = 1; x <= t; x++)); do xdotool click 1; done

Or you might be able to do it without a loop (for this specific utility and function):

xdotool click --repeat 5 --delay 50 1

Simply you can use watch command :

watch -n x <your command>

change x to time in seconds .

Example :

watch -n 1 xdotool click 1 

So xdotool click 1 will be repeated every 1 s , terminate it when your done .

  • 4
    I think the question is specific on running the command a defined number of times, not so much at a defined interval. – Jacob Vlijm Sep 12 '14 at 19:36

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.