-3

I'm trying to execute the command:

echo 'Hello World'

Ten times, but I don't want to type it out ten times, and I thought I could do it with a loop, but I don't know how.

Loseshape
  • 3
  • 3
  • Does this answer your question? [simple while loop in bash](https://stackoverflow.com/questions/21713594/simple-while-loop-in-bash) – Cameron Little Apr 11 '20 at 14:58
  • But.... the example there show (after correction) only 5 lines?..... – Luuk Apr 11 '20 at 15:29
  • @Luuk The loop in the suggested link runs for `x=5` to `9` -- 5 iterations. You'll need to change one or both endpoints of the loop to change the number of iterations. – Gordon Davisson Apr 11 '20 at 20:49
  • Yeah, i know that. but does the asker of this question realize that this is a question for which the answer is easily found? – Luuk Apr 12 '20 at 08:20

1 Answers1

0

You seem new to Bash loops so I will take some time to explain my approach to this question.

Approach

Yes, you can definitely use loops in Bash. And in this example I will use the for loop. I will also use the seq command. When seq receives only one argument, an integer, it will generate numbers 1,2,3 ... until that integer, or in other words until the last number. Since I want Hello World printed ten times, I will just specify 10 as the sole argument of seq. Note that in this case, the number specified is inclusive. More information on the official documentation here.

So my code below does basically this: for every number (stated as i here) from 1 to 10, print the string Hello World.

#!/bin/bash

for i in $(seq 10)
do
    echo "Hello World"
done

And it is basically the same as:

#!/bin/bash

for i in 1 2 3 4 5 6 7 8 9 10 
do
    echo "Hello World"
done
fxdeaway
  • 165
  • 8