More Loops
for iterates words, ranges and files. A C style for uses (( )). while reads lines and until repeats until true. break and continue work as usual.
Example
for ((i = 1; i <= 3; i++)); do echo -n "$i "; done; echo
n=0; until [[ $n -ge 3 ]]; do ((n++)); done; echo $n
printf "a\nb\n" | while read -r l; do echo "got $l"; done
for f in /etc/hostname /nonexistent; do [[ -e $f ]] || { echo "missing $f"; continue; }; echo "have $f"; doneResult
1 2 3 3 got a got b have /etc/hostname missing /nonexistent
Exercise
Repeat until true.
[[ $n -ge 3 ]]; do ((n++)); doneQuiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.