Introduction
A for loop helps you repeat actions for each item in a list. It saves time and avoids writing the same code many times.
for item in list do commands using "$item" done
for color in red green blue do echo "$color" done
for file in *.txt do echo "File: $file" done
for name in "Alice" "Bob Smith" "Charlie" do echo "Hello, $name!" done
#!/bin/bash # List of fruits fruits="apple banana cherry" echo "Fruits before loop: $fruits" for fruit in "$fruits" do echo "I like $fruit" done
for item in apple banana cherry; do echo $item; done
for word in one two three; do echo "$word"; done
for item apple banana cherry; do echo $item; done
fruits="apple banana cherry blueberry" for fruit in $fruits; do # Fill condition here echo "$fruit" done
Pass the test and this pattern moves unknown