How to repeat a shell command N times

How to repeat a shell command N times

Last day, I needed to repeat a shell command a arbitrary number of times, and all I found was the suggestion to use the repeat command.

Sadly, I haven’t been able to found that command in Ubuntu, but after some research I could luckily find something useful. What follows is a very simple bash implementation of the repeat command.

First, open your .bash_aliases file:

xdg-open ~/.bash-aliases

Second, paste these lines at the bottom of the file and save:

repeat() {
    while [ $(( n -= 1 )) -ge 0 ]

Third, either close and open again your terminal, or type:

source ~/.bash_aliases

Et voilà ! You can now use it like this:

repeat 5 echo Hello World !!!


repeat 5 ./

Hope this can help you !

This seems like a great time to subscribe my RSS !

3 responses to “How to repeat a shell command N times”

  1. Evan M

    for i in seq 5; do echo Hello; done

  2. Christophe Muller

    > for i in seq 5; do echo Hello; done


    for i in {1..5}; do echo Hello World \!\!\! ; done


    Also the “repeat” command is a built-in of Csh-type shells, so in Ubuntu you could apt-get install csh or tcsh in order to use it. But then it won’t be bash anymore.. :-)


    = I keep trying to tell my boss that no, Satan is a tool of Linux, not the other way around. –Anonymous =

  3. Alexander Reece

    xargs has proven a valuable tool. Whenever I need to loop over something, xargs often does the job.

    It may seem a bit verbose, (having to pass two parameters to xargs). Overall its pretty short an easy to read.

    seq 1 10 | xargs -I{} -n1 echo ‘Hello World!’

    Those two parameters are the most commonly used parameters for me, as such, I have aliases for them in my .bashrc (listed below). Running over something x times becomes as simple as:

    $ seq 1 10 | xx1 echo “Hello World”

    Want to include a number in your command? Use {} (you may change this in the xargs arguments)
    $ seq 1 10 | xx1 echo “Hello World #{}”

    This not only has the advantage of repeating commands, but you can repeat commands for a variety of loops. (Repeat this once for each directory). I often chain them together:

    # run a command twenty times for each file located beneath the current directory
    $ find -type f -print0 | xx10 seq 1 20 | xx0 echo “Greetings World”

    If you want to get real fancy– multitask:
    $ time find -type f -print0 | xx10 seq 1 20 | xx -P 4 echo “Greetings World”
    real 0m1.339s
    user 0m0.012s
    sys 0m0.212s

    $ time find -type f -print0 | xx10 seq 1 20 | xx echo “Greetings World”
    real 0m2.700s
    user 0m0.040s
    sys 0m0.216s

    Warning: know where your bottlenecks are, otherwise you may run it slower:

    $ time find -type f -print0 | xx10 -P 4 seq 1 20 | xx echo “Greetings World”
    real 0m2.962s
    user 0m0.024s
    sys 0m0.268s

    My useful aliases:
    alias x=”xargs” # x means regular xargs
    alias x0=”xargs -0″ # 0 means that the list is delimited by instead of spaces
    alias x1=”xargs -n1″ # 1 means that there should only be 1 argument per command
    alias x10=”xargs -0 -n1″
    alias x01=”xargs -0 -n1″
    alias xx=”xargs -I{}” # second x means use -I{}
    alias xx0=”xargs -0 -I{}”
    alias xx1=”xargs -n1 -I{}”
    alias xx10=”xargs -0 -n1 -I{}”
    alias xx01=”xargs -0 -n1 -I{}”

Leave a Reply


Don’t forget to Subscribe

Latest Activity


  • Script to enable disable panels auto hide
    You may remember about an old post of mine where I show a little trick to make gnome panel auto-hiding actually usable. I have been pleased, Read More
  • SparkleShare, the new iFolder ?
    Like already stated, the only thing Mono is really worth for, is creating DropBox competitors. Jokes aside, I came around this repository, today:   Will this work, Read More
  • Teeworlds 0.6 released !
    I just learned that one of my favorite games has reached version 0.6. I’m talking about Teeworlds, if you never heard of it, here you Read More
  • Ubuntu 11.04 ‘Natty Narwhal’ Awesome New Plymouth Theme!
    Old but still relevant. Enjoy. Share it !
  • Got diaspora
    Diaspora is an open-source competitor of Facebook. Yesterday I got an invite and here’s how it looks: I’m frankly unimpressed. By the way, If you don’t want Read More