4 Answers
4

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() {
number=$1
shift
for i in `seq $number`; do
$@
done
}

simple for loop yap, but I said no scripts :-)
–
mahatmanichMay 24 '11 at 16:00

8

@mahatmanich, A loop is not a script. There is nothing preventing you from using for... at an interactive terminal.
–
ZoredacheMay 24 '11 at 18:10

2

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 CsabaMay 25 '11 at 8:43

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!