Linux › Section K · Shell scripts › Topic K.5
Temporary files and here documents
Scripts often need scratch space for intermediate results and a tidy way to write out blocks of text. mktemp gives safe, unique temporary files, trap makes sure they are cleaned up however the script ends, and here documents feed whole paragraphs of text to a command.
Why temporary files need care
A script might save a listing now and compare it with another later, or collect output before sorting it. The file must have a name no other program will use. An old habit builds the name from the process ID, as in /tmp/report.$$, but process IDs are easy to guess. In a shared directory such as /tmp, another user could create that name first, perhaps as a symbolic link to one of your files, and your script would then write into the wrong place. The cure is to let a tool choose an unpredictable name and create the file in one step. All outputs below come from real runs in bash 5.2 with GNU coreutils 8.32.
mktemp
mktemp creates a new, empty file with a unique name and prints that name, so you capture it with command substitution: tmp=$(mktemp). With no argument, GNU mktemp uses the template tmp.XXXXXXXXXX in the directory named by TMPDIR, or /tmp when TMPDIR is unset; one run gave /tmp/tmp.joV513XoKc. You can give your own template, whose final X characters are replaced: mktemp /tmp/report.XXXXXX printed /tmp/report.EeRkIg. Too few X's is refused: mktemp /tmp/bad.XX reports too few X's in template and returns 1. mktemp -d makes a directory instead, handy when a script needs several scratch files. The GNU manual states that the file is created readable and writable only by its owner. Store the name in one variable and use "$tmp" everywhere after that.
trap: run commands when the script ends
A temporary file should vanish when the script finishes, including when it fails half-way or someone presses Ctrl+C. The trap builtin registers commands to run when the shell receives a signal or, with the special name EXIT, when it exits for any reason. Write the commands in single quotes, so that variables such as $tmp are expanded when the trap runs rather than when it is set. trap 'rm -f "$tmp"' EXIT is the standard clean-up line. Signals are usually named without the SIG prefix: INT for Ctrl+C, TERM for a plain kill, HUP when a terminal closes. Signals themselves were introduced in "Listing and manipulating processes: signals and jobs".
Worked example: a self-cleaning script
A script tr3.sh must always delete its scratch file. 1. Create it: tmp=$(mktemp). 2. Register clean-up immediately: trap 'rm -f "$tmp"; echo "removed $tmp"' EXIT. 3. Handle termination: trap 'echo "stopped by signal"; exit 143' TERM. The exit matters: without it, the script would carry on after the handler, and 143 is the 128 + 15 code a TERM-killed program normally reports. 4. Do the work: echo started, then sleep 3, then echo "finished normally". 5. Test the hard path: start it in the background, send kill -TERM after one second and wait for it. It printed started, stopped by signal and removed /tmp/tmp. followed by that run's random name, and wait reported status 143. bash ran the handler only once the running sleep had finished. The EXIT trap ran even though the script was stopped, and finished normally never appeared.
More uses of trap
trap is not only for clean-up. trap '' INT, with an empty command, makes the script ignore Ctrl+C, useful around a step that must not be interrupted half-way. trap - INT restores the default behaviour. trap -p lists the traps currently set; after trap '' INT it printed trap -- '' SIGINT. A trap on HUP can save state when a terminal closes. Two signals can never be trapped: KILL (9) and STOP. In bash, a test script with only an EXIT trap still ran it when an untrapped TERM killed it, with status 143. Not every shell promises that, so portable scripts also trap INT and TERM explicitly.
Here documents
A here document feeds lines written inside the script to a command's standard input. cat <<EOF starts it; every following line is input, until a line containing only the marker EOF. Any word can be the marker, and capitals are customary. The shell expands variables and $( ) inside, so with name=Ada, a here document holding Hello, $name. and Today is $(date +%A). printed Hello, Ada. and Today is Friday. on the day of the run. To include a literal dollar sign, escape it, so \$5 printed $5. A here document replaces a long run of echo commands, keeps the text readable, and suits any command that reads standard input: wc -l with a two-line here document prints 2.
Quoted markers, tabs and here-strings
Quote the marker, as in cat <<'EOF', and the shell expands nothing: the same two lines print exactly as written, $name and $(date +%A) included. Use that form for text full of dollar signs, such as a script you are generating. Write <<-EOF and the shell strips leading tab characters, not spaces, from each line and from the closing marker, so a here document can be indented with the code around it. bash also has the here-string, <<< word, which feeds a single string: tr a-z A-Z <<< "here string" printed HERE STRING. The marker line must match exactly: a space after EOF means the document never ends, and bash warns that it reached the end of the file first.
Worked example: generating a configuration file
A script mkconf.sh writes app.conf for a host given as its argument. 1. host=${1:-localhost} uses $1, or localhost when no argument is given, and port=8080 sets the port. 2. Redirect a here document into the file: cat > app.conf <<EOF. 3. The body lines are # generated by $0, server = $host and port = $port, followed by EOF alone on a line. 4. Print a confirmation: echo "wrote app.conf". 5. Run sh mkconf.sh db.example.com. It printed wrote app.conf, and app.conf held # generated by mkconf.sh, server = db.example.com and port = 8080. Check: every variable was expanded because the marker was unquoted; with <<'EOF' the file would contain $host literally.
Common mistakes to avoid
First, building temporary names from $$ in /tmp, which another user can predict. Second, losing the name mktemp printed by not capturing it in a variable. Third, setting the clean-up trap late, after steps that might already fail. Fourth, a signal handler without exit, so the script continues after it. Fifth, writing the trap command in double quotes, which expands variables too early. Sixth, indenting a closing marker with spaces, or adding text after it, so the here document never ends. Seventh, forgetting that an unquoted marker expands $ inside the text.
Key takeaways
Create scratch files with tmp=$(mktemp) or directories with mktemp -d, rather than names built from $$. A template's trailing X characters become random characters. Register clean-up straight away with trap 'rm -f "$tmp"' EXIT, and handle INT and TERM, ending each handler with exit. trap '' INT ignores a signal, trap - INT restores it and trap -p lists traps; KILL cannot be caught. A here document, cmd <<EOF ... EOF, feeds lines to a command and expands variables; <<'EOF' keeps them literal, <<-EOF strips leading tabs, and <<< passes one string in bash.
Flashcards
Flashcard
Creates a unique empty file and stores its name
Test your understanding
Question 1 of 5
Why is mktemp safer than naming a temporary file /tmp/job.$$?
Question 2 of 5
A script sets trap 'echo bye' TERM and is sent kill -TERM while it runs. What happens?
Question 3 of 5
With name=Ada, which here document prints Hi $name exactly as written?
Question 4 of 5
Why should a trap command usually be written in single quotes, as in trap 'rm -f "$tmp"' EXIT?
Question 5 of 5
Which signal can a script never catch with trap?
