Skip to content

Run command

Run command multiple times

for

https://serverfault.com/a/273241

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
}

Usage:

run 10 command

Example:

run 5 echo 'Hello World!'

https://stackoverflow.com/a/3737773

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

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

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

seq

Shell下重复多次执行命令:

seq 10 | xargs -i date
Fri Jul 21 14:20:55 CST 2017
Fri Jul 21 14:20:55 CST 2017
Fri Jul 21 14:20:55 CST 2017
Fri Jul 21 14:20:55 CST 2017
Fri Jul 21 14:20:55 CST 2017
Fri Jul 21 14:20:55 CST 2017
Fri Jul 21 14:20:55 CST 2017
Fri Jul 21 14:20:55 CST 2017
Fri Jul 21 14:20:55 CST 2017
Fri Jul 21 14:20:55 CST 2017

seq 生成一个1-10的数组。xargs兴高彩烈的拿着这个数组,准备将数组元素传给date 命令做参数。xargs -i会查找命令字符串中的{},并用数组元素替换{}。但是xargs一看,WTF!命令里面居然没有占位符。那好吧,就执行10遍命令,让参数随风去吧。

Run command periodically

watch

man 1 watch

Shell下重复多次执行命令

watch -n 1 date

while true; do ... ; sleep ...; done

Shell下重复多次执行命令

$ while true; do date; sleep 1; done
Fri Jul 21 14:20:24 CST 2017
Fri Jul 21 14:20:25 CST 2017
Fri Jul 21 14:20:26 CST 2017
Fri Jul 21 14:20:27 CST 2017