AnyCompiler

Loops

for var in ... ; do ... done iterates over a list of words directly, no index variable needed.

for color in red green blue; do
  echo "$color"
done
Output
red
green
blue

while condition; do ... done repeats as long as condition succeeds (exit code 0). $((i + 1)) is arithmetic expansion, covered in full later in this course — here it just increments i each pass.

i=0
while [ "$i" -lt 3 ]; do
  echo "i=$i"
  i=$((i + 1))
done
Output
i=0
i=1
i=2

Next: arrays — Bash's ordered list type, indexing, and length.

Practice exercise

Using a for loop over {1..5}, add up the numbers 1 through 5 into a variable sum starting at 0, then print sum.