Linux – How to install ‘repeat’ on Ubuntu

bashcommand-line-interfacedebianlinuxUbuntu

This StackOverflow question mentions a unix command called 'repeat'. It sounds like it does exactly what I want. From reading the question and answers, I think the user is on Mac OSX.

However that command is not installed by default on Ubuntu, and I can't find the package to install to get it. What should I install?

Best Answer

I can't find this command on Ubuntu. It doesn't seem to exist. I even find it very weird that the post on StackOverflow says it's a builtin command when I can't find it on Ubuntu.

Edit: Like Matt noted, it is a builtin csh command. The following are tips to do quite the same with bash.

If what you want is to repeat a command n times, you can do that with a loop though:

for i in {1..n}; do yourcommand; done

For example, to print 100 times "It works", use:

for i in {1..100}; do echo "It works"; done

If you want to have a repeat function, you could add something like this to your ~/.bashrc:

function repeat() { 
    local times="$1"; 
    shift; 
    local cmd="$@"; 

    for ((i = 1; i <= $times; i++ )); do 
       eval "$cmd"; 
    done 
 }

Source your ~/.bashrc again with . ~/.bashrc and you can call it:

 $ repeat 2 date
Mon Dec 21 14:25:50 CET 2009
Mon Dec 21 14:25:50 CET 2009

 $ repeat 3 echo "my name is $USER"
my name is raphink
my name is raphink
my name is raphink