The problem with fixed names
A fixed path such as /tmp/work.txt has three problems. Two runs of the same script at the same time share the file. A stale file from an earlier crash can contaminate the new run. And in a directory that everybody can write to, another user can create the file first and make the script read or write data it should not touch. Predictable names in shared temporary space are a classic security bug.
echo "data" > /tmp/work.txt
cat /tmp/work.txtThis works on a single run, but the file name is the same on every run. A second user on a shared machine who knows the name can create that file first, and then your script writes to the file they chose, or they may replace it with a link to a file you did not mean to touch. The fix is not to pick a more complicated name by hand. It is to let the system create the name for you.
Creating a temporary file
mktemp creates a file with a unique name and prints the name. The file is created with permissions that only the owner can read and write, which is mode 600. If you give it a template, the trailing run of X characters is replaced by random characters. The template should include a readable prefix so that a leftover file is easy to recognize.
f=$(mktemp /tmp/report.XXXXXX)
echo "created: $f"
ls -l "$f"The first line prints a path such as /tmp/report.a1B2c3, with random characters in place of the Xs. The listing shows the file belongs to you and has permissions that only you can read and write. The exact name differs on every run, so do not write scripts that expect a particular value.
Creating a temporary directory
The -d option creates a directory instead of a file. Use it when a step writes several files, such as a build that unpacks an archive. The directory is also created with mode 700, so only your user can enter it.
d=$(mktemp -d /tmp/build.XXXXXX)
echo "hello" > "$d/note.txt"
cat "$d/note.txt"
rm -rf "$d"The script prints hello and then removes the whole directory. Always quote the variable in removal commands. A directory name with a space that is unquoted would be split, and rm -rf on the wrong words is one of the most damaging mistakes a shell can make.
Never run rm -rf on a variable that might be empty. Combine this unit with the strict mode unit: set -u stops the script on an unset name, and a check such as [[ -n $d ]] protects against an empty value that is set.
Combining mktemp with a trap
A temporary file is only useful if it is removed when the script ends, including when it fails. The pattern that solves this is to create the file, then register a trap that removes it, as the previous unit showed. Put both lines together, so that no path through the script can skip the cleanup.
tmp=$(mktemp /tmp/job.XXXXXX)
trap 'rm -f "$tmp"' EXIT
echo "step one" > "$tmp"
cat "$tmp"The output is step one, and the file is removed when the script exits. The trap string uses single quotes so that the variable is read when the trap runs.
Where mktemp puts files
When the template has no directory part, mktemp uses the directory named by the TMPDIR environment variable, and falls back to /tmp. That lets users redirect scratch space with one variable. A template that includes a directory, as in the examples above, always uses that directory.
Hands-on: make a temporary file
Run the block below. It creates a file, shows its permissions, and removes it. Notice that the permissions are the same no matter where the file is created.
t=$(mktemp /tmp/demo.XXXXXX)
stat -c "%a %n" "$t"
rm -f "$t"The output is the mode 600 followed by the path. The lab for this unit asks you to write a script that creates a temporary directory and removes it on exit, and it checks that the directory is gone.