1

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]
David542
  • 104,438
  • 178
  • 489
  • 842
  • 5
    Why don't you want to do a loop? That is the normal way to do something like this. – Kevin Mar 12 '15 at 19:20
  • 1
    Your example is actually different to a for loop, `&&` will break the chain if a command has a non zero return. Is this what you actually want? I.e. a for loop that breaks on error – texasflood Mar 12 '15 at 19:27
  • Use a `while` loop. Possible duplicate of [Is there a better way to run a command N times in bash?](https://stackoverflow.com/q/3737740/608639) – jww Aug 30 '18 at 01:17

2 Answers2

7

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
Brian Campbell
  • 322,767
  • 57
  • 360
  • 340
4

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 }'
codess
  • 356
  • 1
  • 6