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.
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.
You seem new to Bash
loops so I will take some time to explain my approach to this question.
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