LinuxCommandLibrary

done

Marks end of loop or conditional block

TLDR

View documentation for the for keyword

$ tldr for
copy

View documentation for the while keyword
$ tldr while
copy

View documentation for the select keyword
$ tldr select
copy

View documentation for the until keyword
$ tldr until
copy

SYNOPSIS

for variable in list; do commands; done
while condition; do commands; done
until condition; do commands; done

DESCRIPTION

The done command in Linux, while not directly executable from the command line like 'ls' or 'grep', is a crucial keyword used within shell scripts, particularly in conjunction with loop constructs like for, while, and until. It signifies the end of the loop's body.

When a loop statement encounters the done keyword, it marks the completion of one iteration (or the entire loop if the exit condition is met). The shell then either begins the next iteration or exits the loop, depending on the loop type and the evaluation of any associated conditions.

Without done, the shell interpreter would not know where the loop ends, resulting in syntax errors and unpredictable behavior. It's a fundamental structural element for iterative tasks within shell scripting.

CAVEATS

done is a keyword, not a standalone command. It's *always* used in conjunction with loop statements.

EXAMPLE USAGE

for i in 1 2 3; do echo "Number: $i"; done
This simple for loop will print "Number: 1", "Number: 2", and "Number: 3" each on a new line. The done signals the end of each iteration.

NESTED LOOPS

done can be used in nested loops (loops within loops). Each done keyword closes the corresponding loop that started with for, while, or until.

HISTORY

The done keyword is a core part of the Bourne shell (sh) and has been a standard feature of shell scripting since its inception in the late 1970s. It provides a structured way to define the boundaries of loops, allowing for efficient and readable script development. It has been consistently present in all POSIX compliant shells.

SEE ALSO

for(1), while(1), until(1), if(1), break(1), continue(1)

Copied to clipboard