Loops & Functions

Loops

for f in *.log; do echo "$f"; done

for i in $(seq 1 5); do echo "$i"; done

while read -r line; do echo "$line"; done < input.txt

until ping -c1 host &>/dev/null; do sleep 2; done

Functions

greet() {
  local who="$1"
  echo "Hi, $who"
  return 0
}
greet Alice

case statement:

case "$1" in
  start) echo starting ;;
  stop)  echo stopping ;;
  *)     echo 'usage: start|stop' ;;
esac
Exam tip: while read -r line; do ...; done < file is the canonical way to process a file line by line. -r stops backslash mangling.