Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Is there a way to run a command (e.g. ps aux|grep someprocess) for n times?

Something like:

run -n 10  'ps aux|grep someprocess'

I want to use it interactively, please do not post scripts.

Update: The reason I am asking this is, that I do work on a lot of machines and I don't want to import all my adaped scripts etc into every box to get the same functionality accross every machine.

share|improve this question
Wonderful that the answer get such many upvotes without the question even receiving one. Let's change that +1 :) –  Lekensteyn May 26 '11 at 15:09
@Lekensteyn thanks –  mahatmanich May 26 '11 at 15:23

5 Answers 5

up vote 76 down vote accepted

I don't think a command or shell builtin for this exists, as it's a trivial subset of what the Bourne shell for loop is designed for and implementing a command like this yourself is therefore quite simple.

For starters you can use a dummy for loop:

for i in `seq 10`; do command; done

Or equivalently as per JimB's suggestion, using the Bash builtin for generating sequences:

for i in {1..10}; do command; done

This iterates ten times executing command each time - it can be a pipe or a series of commands separated by ; or &&. You can use the $i variable to know which iteration you're in.

If you consider this one-liner a script and so for some unspecified (but perhaps valid) reason undesireable you can implement it as a command, perhaps something like this on your .bashrc (untested):

#function run
run() {
    for i in `seq $number`; do


run 10 command


run 5 echo 'Hello World!'
share|improve this answer
simple for loop yap, but I said no scripts :-) –  mahatmanich May 24 '11 at 16:00
@mahatmanich, A loop is not a script. There is nothing preventing you from using for... at an interactive terminal. –  Zoredache May 24 '11 at 18:10
Well, the one-liner above is the kind of standard way to do it and it is fairly simple. Why is it not good for you? Maybe you are asking the wrong question? What is the main goal of your scripts or your actions that you want to repeat them a number of times? Maybe there is a better solution if we put the problem in a different way. –  Patkos Csaba May 25 '11 at 8:43
@mahatmanich - for is the bash built-in for iterating. –  JimB May 25 '11 at 12:58
@Eduardo Ivanec - FYI, bash has built-in range like seq: {1..10} –  JimB May 25 '11 at 13:03

ps aux | grep someprocess looks like you want to watch changes of a program for a fixed time. Eduardo gave an answer that answer your question exactly but there is an alternative: watch:

watch 'ps aux | grep someprocess'

Note that I've put the command to avoid the shell from interpreting the command as "run watch ps auxand pipe the result throughgrep someprocess`. Another way to do the previous command would be:

watch ps aux \| grep someprocess

By default, watch refreshes every two seconds, that can be changed using the -n option. For instance, if want to have an interval of 1 second:

watch -n 1 'ps aux | grep someprocess'
share|improve this answer
hi thanks, we already had a watch answer here but it mystically disappeared from the page. –  mahatmanich May 26 '11 at 15:21
Yes this works like a charm. –  Jānis Gruzis Dec 21 '12 at 14:08

Just for fun

pgrep ssh ;!!;!!;!!;!!;!!;!!
share|improve this answer
Can someone explain this please? Looks very intriguing. –  Daniel Vartanov Feb 24 '14 at 16:31
; is just command separator. !! replay last command in bash. So this run 'pgrep ssh' and then replay it 6 times. –  Piotr Kukielka Sep 21 '14 at 19:11
While that is instructive is not vry intuitive. One-liner for seems the best way. Anyway thanks for the answer. –  erm3nda Mar 21 at 6:27
Won't cmd ;!! on one line just repeat the command entered before the current line? It'd have to be two separate history entries. –  Joel Purra Apr 28 at 7:16

Try this:

yes ls | head -n5 | bash

This requires the command to be executed in a sub-shell, a slight performance penalty. YMMV. Basically, you get the "yes" command to repeat the string "ls" N times; while "head -n5" terminated the loop at 5 repeats. The final pipe sends the command to the shell of your choice.

Incidentally csh-like shells have a built-in "repeat" command. You could use that to execute your command in a bash sub-shell!

share|improve this answer

POSIX says:

while [ $x -gt 0 ]; do

which could of course be made into a one liner:

x=10; while [ $x -gt 0 ]; do command; x=$(($x-1)); done

and seems to be the most portable way, and thus less likely to make you install programs.

The following are less portable:

  • brace expansion {1..10}: bash specific, and would generate a huge line if the argument is large
  • seq: GNU
  • yes: GNU

And if you get tired of portability, consider GNU parallel:

sudo apt-get install parallel
seq 100 | parallel echo {}

which runs commands in parallel and has many cool options.

share|improve this answer

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.