Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Suppose I have a terminal command called foo (in reality this would be something like cd or ls or something more complicated. Is there a way that I can perform this command numerous times WITHOUT the use of a script.

For instance, I want to do the equivalent of the following:

foo && foo && foo && foo && foo

Is there some way that I can do something along the lines of

foo * 5 

or maybe

5:foo

I'm using a linux machine.

share|improve this question
    
In addition to for, you could use watch, as mentioned in this StackOverflow question –  ernie Dec 6 '13 at 20:03

5 Answers 5

Add some semi-comma into blogger's answer to make it as one-liner:

for variables in {1 2 3 4 5};  do ls; done
share|improve this answer

blogger is right. however, it's important to note the syntax exactly: if you're using an interactive shell (e.g. the terminal)

echo -n; for i in {1..5}; do if [[ $? ]]; then foo; fi; done

the "$?" returns the status of the previous function call; because we're calling "echo" first, the first iteration of the loop will be "true" (0 in bash.) the "fi" closes off the "if" statement, and the semicolons are necessary.

here's the same thing if you were writing a bash script:

#!/bin/bash
echo -n
for i in {1..5}; do
if [[ $? ]]; then
foo
fi
done

you'll notice that foo doesn't have a semicolon after it; when writing a full-blown bash script in an environment where line-breaks are available (e.g. your favorite text editor,) line-breaks do the same thing as semicolons :)

also, note that you could instead use

[[ $? ]] && foo

instead of the if statement; realize that the && means that foo will only be executed if the previous foo didn't have an error. i.e. in the function call

thing1 && thing2

thing2 will only be executed if thing1 runs fine (technically, if it returns a 0 value) to ignore whether foo is working or not, use semicolons instead of &&

share|improve this answer

You can try something like:

for i in {1..10}; do YourCommand; done

Example:

for i in {1..5}; do ls -l; done

share|improve this answer

Have you tried using "for" loop?

for VARIABLE in 1 2 3 4 5 .. N
do
    command1
    command2
    commandN
done
share|improve this answer
    
When I try to do that, it just brings up a new line that says for> –  Nosrettap Dec 6 '13 at 19:24

I found a great method here.

I know it's still "kind of a script" but i think it's the closest you can get without typing a whole command line with commands.

You can create the command repeat with which you can do this:

 repeat 5 ./foo

Add the following lines to the end of your ~/.bashrc (if you're using bash):

repeat() {
    n=$1
    shift
    while [ $(( n -= 1 )) -ge 0 ]
    do
        "$@"
    done
}

and the next time you login (or start bash) you can use repeat 5 ./foo.

Btw. repeat is already a keyword (which does exactly the same) in csh, tcsh and zsh, so with this addition you will add it to bash and it will work exactly the same.

share|improve this answer

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.