Loop de loop
Sometimes you have scripts with some repetition. To avoid copy-pasting, you can write loops. Bash has a couple of different ways of writing loops.
for-loop
for ((i = 1; i < 10; i++)); do
echo $i
done
for-in (aka for-each)
for i in {1..10}; do
echo $i
done
or
for i in "foo" "bar" "baz"; do
echo $i
done
while-loop
i=1
while [ $i -le 10 ]; do
echo $i
((i++))
done
The ((i++)) part reassign the variable $i to i+1.
until-loop
until [ $i -gt 10 ]; do
echo $i
((i++))
done
Challenge
Write a script that draws a Christmas tree out of * using a loop.
Example output:
*
***
*****
*******
*