utility

mktemp

Create a fresh directory with an unpredictable suffix. Check status and remove only owned fixture contents. Template rules vary.

Formal Syntax & Structure

bash syntaxbash
mktemp -d TEMPLATE

Parameters & Attributes

NameTypeDescription
TEMPLATEstringTemplate containing trailing X characters.
-doptionCreate a directory.

Return Value

exit status

Exit status 0 on success; nonzero indicates failure or a command-specific condition. See description and example.

Working Example

Example Snippetbash
work=$(mktemp -d ./bm.XXXXXXXX) || exit 1printf 'temporary\n' > "$work/note.txt"cat -- "$work/note.txt"rm -- "$work/note.txt" && rmdir -- "$work" || exit 1printf 'cleaned\n'
Bash Reference