David542
David542

Reputation: 110153

Run a command N number of times

Without doing a for loop in bash is there a way to execute a command N times? For example:

$ pulldown 123 && pulldown 123 && # ...etc [100 times]

Upvotes: 1

Views: 5412

Answers (2)

Brian Campbell
Brian Campbell

Reputation: 332816

You can use a for loop and simply ignore the argument:

for i in $(seq 1 100); do pulldown 123; done

If you absolutely can't use a for loop for some reason, you could use xargs -n 1 and ignore the argument, with something like the following; the -n 1 indicates to run the command once per line of input, and the -I XXX means replace all occurrences of XXX in the command with the current input line (but since we don't use that in the command, it is ignored):

seq 1 100 | xargs -n 1 -I XXX pulldown 123

Upvotes: 7

codess
codess

Reputation: 366

A while loop could be used, something like this:

i=0
while [ $i -lt 100 ]; do
        echo $i
        i=`expr $i + 1`
done

Alternatively, ruby could possibly be used with the -e switch, like this:

$ ruby -e '100.times { |i| puts i }'

Upvotes: 4

Related Questions