Is there a better way to run a command N times in bash? Is there a better way to run a command N times in bash? bash bash

Is there a better way to run a command N times in bash?


If your range has a variable, use seq, like this:

count=10for i in $(seq $count); do    commanddone

Simply:

for run in {1..10}; do  commanddone

Or as a one-liner, for those that want to copy and paste easily:

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


Using a constant:

for ((n=0;n<10;n++)); do    some_command; done

Using a variable (can include math expressions):

x=10; for ((n=0; n < (x / 2); n++)); do some_command; done


Another simple way to hack it:

seq 20 | xargs -Iz echo "Hi there"

run echo 20 times.


Notice that seq 20 | xargs -Iz echo "Hi there z" would output:

Hi there 1
Hi there 2
...