1

According to this question, which said

"How to run a command multiple times?",

the correct answer was

for i in `seq 10`; do command; done

Now, If the command has an argument and every iteration, we should pass this argument to the command automatically.

How can we perform this in Linux terminal?

thanks.

migrated from serverfault.com Sep 14 '16 at 20:40

This question came from our site for system and network administrators.

  • 3
    How do you determine what the argument is for each command. If you want the number of the iteration from your example, you can just run command "$i", but if you need that argument to come from some other place, you'll have to tell us how to know the right argument for each iteration of the loop – Eric Renouf Sep 14 '16 at 14:26
  • Thank you @EricRenouf your suggestion of using $i is correct. How can I point to your comment as a correct answer? – husin alhaj ahmade Sep 14 '16 at 17:01
3

With the loop you reference in your command, you are storing the next "word" from the seq command in the variable i. You can use that value anywhere you like, so to pass it to the command you can invoke it as command "$i"

You can avoid the need for the extra seq process with bash at least you can do it like

for ((i=1; i<=10; i++)); do
    command "$i"
done

or with brace expansion like

for i in {1..10}; do

or if you want to do it with POSIX compliance you could do something like

i=1
while [ "$i" -lt 11 ]; do
    command "$i"
    i=$((i+1))
done
0
for i in `seq 10`; do command $i; done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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