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

do
  [commands]
done

DESCRIPTION

The done reserved word in POSIX shells (Bash, Dash, Zsh, Ksh) terminates the body of a loop started by do. It is essential for constructs like for, while, and until, marking the end of repeated commands.

Without done, the shell parser encounters syntax errors as it cannot delineate loop bodies. It has no arguments or options; its presence alone signals loop termination.

Example usage:
for file in *.txt; do
  echo "Processing $file";
done

This iterates over .txt files, echoing each. Nesting is supported, with each do requiring a matching done. Mismatched pairs cause syntax error: unexpected end of file or similar.

done processes sequentially; loops run in current shell context, inheriting variables and traps. Redirects apply to entire bodies if placed before do. It's a core scripting element for automation, file processing, and control flow.

CAVEATS

Requires matching do; nesting must balance precisely. No standalone use; syntax error otherwise. Ignores trailing arguments silently in Bash.

BASIC LOOP EXAMPLE

while read line; do
  echo "$line" | tr 'a-z' 'A-Z';
done < input.txt

Converts file to uppercase line-by-line.

NESTED LOOPS

for dir in /tmp/*; do
  for file in "$dir"/*; do
    ls -l "$file";
  done;
done

HISTORY

Originated in Bourne shell (1977, Version 7 Unix). Standardized in POSIX.1-1988; evolved minimally across shells for compatibility.

SEE ALSO

bash(1), dash(1), zsh(1), ksh(1)

Copied to clipboard