Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

3 Answers 3

up vote 6 down vote accepted

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
share|improve this answer
    
It's a "shell builtin", which means it's sort of like "echo" in that although there is a /bin/echo, if you just type "echo", it doesn't get executed. bash (or whatever your shell is) has an "echo" command that it runs instead, which prevents the system from having to launch another process. –  Matt Simmons Dec 21 '09 at 13:22
    
Although your way works as well –  Matt Simmons Dec 21 '09 at 13:23
    
Yes Matt, I read your comment thanks. However, it's not a builtin in bash. The command doesn't exist when I use bash. –  ℝaphink Dec 21 '09 at 13:25
    
You can avoid calling the external seq by using for ((i = 1; i <= $times; i++ )) –  Dennis Williamson Dec 21 '09 at 13:36
    
Yes, that's probably more efficient Dennis, although I find the seq syntax more readable somehow. –  ℝaphink Dec 21 '09 at 13:41
show 2 more comments

You could use watch, which is a standard command available in any shell. For example:

watch -n 5 date
share|improve this answer
add comment

From the prompt, I'd guess it's a csh builtin.

And from reading "man csh", that appears to be the case

  repeat count command
           The specified command, which is subject to  the  same  restric-
           tions  as  the  command  in the one line if statement above, is
           executed count times.  I/O  redirections  occur  exactly  once,
           even if count is 0.

So in order to use it, either type "csh" and issue it from the command line, or write your script so that it uses #!/bin/csh as the interpreter at the top. Here are some csh basics to get you started.

share|improve this answer
2  
Better yet- don't get started on an obsolete, incompatible shell. Learn real shell programming and write yourself a repeat alias or function in Bash, a (mostly) Posix standard shell. –  kmarsh Dec 21 '09 at 13:41
1  
Eh. I'm a bash guy, but csh doesn't bother me. I know a lot of people that would say the exact same thing as you, except change csh to bash and bash to korn. There's a lot of truth to the fact that korn is more advanced than bash. It's all what you're comfortable with and what gets the job done. csh is going to be around for a long, long time –  Matt Simmons Dec 21 '09 at 13:47
    
Or you could change csh to bash and bash to zsh. –  Joseph Kern Dec 21 '09 at 16:11
    
While quite a few people have their favorite shell, I find that most sysadmins know bash, while there's few that know csh, zsh and others, and since Ubuntu comes with bash by default (for the users at least, it has dash for root), it's still nicer to play with bash when possible. That's just my opinion though. –  ℝaphink Dec 21 '09 at 21:12
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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