This is probably dead easy, but is there a simple way to write a command once in the console, and have it executed n times, where n is specified at runtime? Something like this:

repeat 100 echo hello

Does such command exist (assume typical Linux installation)?

Or would I write to do some kind of loop in bash?

link|improve this question

80% accept rate
feedback

3 Answers

up vote 4 down vote accepted

Yes this is possible. Bash has a very extensive scripting language. In this case:

for i in {1..100}; do echo 'hello'; done

More looping examples: http://www.cyberciti.biz/faq/bash-for-loop/
Full bash reference: http://www.gnu.org/software/bash/manual/bashref.html

link|improve this answer
feedback

Or would I write to do some kind of loop in bash?

Yes, you would, like this:

for(( i = 0; i < 100; i++ )); do echo "hello"; done

or, shorter:

for((i=100;i--;)); do echo "hello"; done
link|improve this answer
and then put that stuff into a function and voila, you have your command: repeat() { for_stuff_here ; do "$@"; done } – akira Mar 22 '10 at 15:03
feedback

In addition to more built in methods you could use an external utility that generates a sequence of numbers.

# gnu coreutils provides seq
for i in $(seq 1 100) ; do printf "hello\n" ; done

# freebsd (and probably other bsd) provides jot
for i in $(jot - 1 100) ; do printf "hello\n" ; done
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.