Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Loops and Functions  ›  Lesson

while and until Loops

while 14 minA while loop repeats while its test command succeeds and an until loop repeats while it fails, and while read is the standard way to process a file line by line.

while and until Loops

The folder list for backup.sh is growing, and you would rather keep it in a file, folders.txt, than inside the script. You do not know how many lines it has. A for loop walks a list you already have; a while loop keeps going as long as something is true, such as "there is another line to read".

The rule

while repeats as long as its test succeeds; until, as long as it fails.

Like if, the test is a command, and "succeeds" means exit status 0:

n=3
while (( n > 0 )); do
    echo "$n"
    (( n-- ))
done                      # prints 3, 2, 1

until ping -c 1 backup-server >/dev/null; do
    sleep 5               # keep trying until the server answers
done

The most important while in Bash reads a file line by line:

count=0
while IFS= read -r line; do
    tar -czf "$dest/$count.tar.gz" "$line" && (( count++ ))
done < folders.txt
echo "Archived $count folders"
  • read succeeds while there is a line and fails at the end of the file, which ends the loop.
  • IFS= stops read trimming spaces at the ends; -r keeps backslashes.
  • < folders.txt after done feeds the file to the whole loop.

while true; do ... done loops forever, until something inside uses break or exit.

The shortcut

Check the test before every pass, including the first. Say it aloud: test, body, test, body, ... test fails, stop. For until, flip it: stop when the test succeeds. And make sure something in the body changes the test, or the loop never ends.

until [[ $n -ge 3 ]] with n=0Step 1 of 4
  1. Test before pass 1

    Is 0 at least 3? No, the test fails, so until runs the body. n becomes 1.

A classic trap: cat folders.txt piped into while read .... The pipe runs the loop in a subshell, so count is back to 0 after done. Redirect with done < file instead.

Drills

Answer fast — trust the rule.

quick drill

What does n=3; while (( n > 0 )); do echo -n "$n "; (( n-- )); done print?

quick drill

With n=0, how many times does the body of until [[ $n -ge 3 ]]; do (( n++ )); done run?

quick drill

Where does the file go in a while read loop that reads folders.txt?

quick drill

Why write IFS= read -r line rather than plain read line?

Remember

  • while repeats as long as its test succeeds; until, as long as it fails.
  • The test runs before every pass; something in the body must change it.
  • Read files with while IFS= read -r line; do ...; done < file, not through a pipe.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainRead the folder list from a file

backup.sh should read its folders from folders.txt, one path per line (some contain spaces), and count how many it archived. Write the loop and explain each part of while IFS= read -r line, where the file is connected, and why piping the file into the loop would lose the count.