# Temporary files and code golf

In shell scripts, one can oftentimes save a few bytes by creating temporary files instead of storing information in variables. Given that the lion share of the work is usually done by invoking external programs, this is not only the golfiest, but the most natural choice.

For example, instead of the Bash script

t=command1
echo -n "$t"|command2 echo -n "$t"|command3


one could use

command1>t
command2<t
command3<t


which is guaranteed to work as intended and a whopping 24 bytes shorter.

Now, a production-level Bash script would create a temporary file or directory with mktemp and clean up after itself before exiting, but what should a code golf answer do?

1. Where should programs be allowed to create temporary files?

In particular, is it OK to simply create a file in the current directory, possibly overwriting other files in the process?1

2. Do programs have to delete the files they created in the current directory (if allowed)?

Do programs have to delete the files they created in /tmp (which the system should eventually delete on its own)?

1 It goes without saying that the answer should include a heads-up in this case.

• I'm not too sure what to answer, but to me it sounds similar to outputting additional unwanted things to stdout, which is not allowed, except that here it's in files instead of stdout. But an exception could be made if it's a "natural" way to use the language (which it seems to be) Oct 7, 2015 at 8:50
• Since we allow programs to be run on a fresh interpreter, I think we can safely assume that the directory has no unwanted files, and not worry about cleanup. Oct 8, 2015 at 4:40

# Yes, this is valid

I think that outputting information to files are okay, as long as the asker doesn't specifically tell you that it's not allowed.

# Yes, but use mktemp

mktemp -d will create a temporary directory in $tmp (usually /tmp) and print the name to stdout. The 14 extra bytes needed to call it and assign to a single-letter variable (d=$(mktemp -d)) is a fair price to pay for the use of temporary files, when other languages are typically not allowed to use them.

• mktemp uses \$TMPDIR if set and /tmp otherwise. In most cases, cd mktemp -d; is probably shorter. Oct 11, 2015 at 6:35
• That works too.
– user45941
Oct 11, 2015 at 7:48

# Yes, but list them differently

It is always allowed to use mktemp or /tmp itself. But if the program use temporary files in the current directory, it should be listed as "Bash (with temporary files)" or something like that, if it is not the usual way using this language.