mktemp

The command that safely creates a new, uniquely named temporary file, or with -d a directory, and prints its path.

In more detail

Names like /tmp/myscript.tmp can collide with another run or be hijacked by another user, while mktemp picks an unpredictable name and creates it only readable by you. Pair it with an EXIT trap that removes it.

Example

work="$(mktemp -d)"; trap 'rm -rf "$work"' EXIT

Related terms

Taught in

References