Creating a temporary file

Bash:
Creating a temporary file

How to:

Bash has a built-in command called mktemp to make temporary files easily:

# Create a temporary file
temp_file=$(mktemp)

# Check out our fresh temporary file
echo "Temporary file created: $temp_file"

# Use the temporary file
echo "Some data" > "$temp_file"

# Read it back
cat "$temp_file"

# Tidy up: remove the file when you're done
rm "$temp_file"

Output:

Temporary file created: /tmp/tmp.Iy5nv69sed
Some data

Deep Dive

Temporary files have been in UNIX since the early days, letting users deal with intermediate data without manual cleanup. In Bash scripting, mktemp is the modern take, coming with options for creating both files (mktemp) and directories (mktemp -d). The command makes a unique file every time it’s called, which dodges file collision issues that happen when multiple instances of a script or different scripts are running at the same time.

Before mktemp, programmers would manually create files with names they hoped would be unique. Clashes were common, leading to data loss and security issues. mktemp helps prevent that by ensuring that the filename is unique with a mix of predictable patterns and random characters. Unlike regular files, these temporary ones are meant to be deleted after use, keeping the system tidy.

Some alternatives to mktemp include using /dev/shm for in-memory temp files, or crafting one with date and process ID ($$), but these methods come with more risks of conflicts.

See Also