Skip to content

What is done Linux command?

The done command in Linux is used to notify the shell that a background task has completed. It is primarily used in shell scripts to mark the end of a background task.

Terminal window
rsync [options] source destination
OptionDescription
-vVerbose output showing the files being transferred
-rRecursively transfer files and directories
-aArchive mode; preserves permissions, timestamps, etc.
-zCompress file data during transfer
-hOutput numbers in a human-readable format
-PShows progress during file transfer
-nPerform a trial run with no changes made
-uUpdate: skip files that are newer on the destination
-eSpecify the remote shell to use
ParameterDescription
sourceThe source file or directory to copy
destinationThe destination where files will be copied to
Terminal window
done task1

Marks the task named “task1” as completed.

Terminal window
done task2 task3 task4

Marks multiple tasks (“task2”, “task3”, and “task4”) as done in one command.

Terminal window
done task*

Uses a regular expression to mark all tasks starting with “task” as done.

Terminal window
done -l "task5: Completed with notes"

Marks the task “task5” as done and adds the note “Completed with notes”.

Terminal window
done -r

Repeats the action of marking the last task as done.

To use the done command in bash, execute the following command:

Terminal window
for i in {1..5}; do echo $i; done

The done keyword marks the end of a script’s loop construct, such as a for loop or while loop.

Terminal window
while condition; do
# Commands to execute
done

How to properly close a loop in bash using done?

Section titled “How to properly close a loop in bash using done?”

To properly close a loop in bash using the done command, ensure that the corresponding loop structure is completed and that the done keyword is correctly placed to mark the end of the loop.

Terminal window
for file in *.txt; do
echo "Processing file: $file"
done

Can the done command be used outside of loops in bash?

Section titled “Can the done command be used outside of loops in bash?”

No, the done command is specifically used to mark the end of loop constructs in bash scripts. It is not used outside of loops.

Terminal window
for i in {1..3}; do
if [ $i -eq 2 ]; then
echo "Found number 2"
fi
done

When using nested loops in bash, each inner loop must have its corresponding done keyword to properly mark the end of the loop construct.

Terminal window
for i in {1..3}; do
for j in {a..c}; do
echo "Combination: $i$j"
done
done

Is it necessary to use done after a while loop in bash?

Section titled “Is it necessary to use done after a while loop in bash?”

Yes, the done keyword is essential to mark the end of a while loop in bash. If omitted, it will result in a syntax error.

Terminal window
while read line; do
echo "$line"
done < file.txt
Section titled “How to handle errors related to the done command in bash scripts?”

To handle errors related to the done command in bash scripts, carefully check for correct placement and matching with the corresponding loop structure, ensuring there are no missing or extra done keywords.

Terminal window
for (( i=1; i<=5; i++ )); do
echo "Iteration: $i"
done
  • Terminating a loop in a bash script
  • Marking the end of a group of commands in a script
  • Used in conjunction with the for, while, or until loop constructs