All users share
so you should make unique filenames there.
The best way to do this is by putting
in the filename.
"/tmp/jerry.12345" [New file]
The shell replaces
with the shell's
PID number (38.3
(in this case,
If you use a
or have more than one login session or window,
and want to share the same temp file,
won't work for you.
In that case, just pick a unique name.
You could use today's date instead.
To give yourself both options with a minimum of work,
here are lines for your
shell setup files (2.2
The left column has lines for csh
-like shells, and the
right is for sh
set tf=/tmp/jp$$ tf=/tmp/jp$$
set date = (`date`) set `date`
setenv TF /tmp/jp$date TF=/tmp/jp$4
(The last two lines grab the fourth word - the current time - from the
output of the
When I want a temporary file in my current shell, I type:
grep foo bar > $tf-1
grep wheeze bar > $tf-2
The shell expands the
shell variable (6.8
into a filename like
expands into all my temporary files in this shell.
Usually, that's great. But if I go to a subshell, do a shell escape,
and so on, the temporary files I make with
won't be the
same as the ones I make in my login shell because the PIDs are
different. If I need them to be the same, I use
environment variable (6.1
It's set to the time I logged in.
And because environment variables are passed to child shells, the name
) will be the same in subshells:
someprog > $TF-1
otherprog > $TF-6
If I'll be using a file for more than a minute or two, I might forget
what's in which file.
So I leave myself a note in shell variables named
and environment variables named
-where "xf" means
"explain file" and
, etc. to correspond to the variable.
If I don't remember which have what, I get a list by piping the output of
(for shell variables) or
(for environment variables) through grep
sort -t: +2 $tf-2 > $tf-3
set xf3='sorted list of chapter 21 files'
set | grep xf
xf1 sorted list of chapter 20 files
xf3 sorted list of chapter 21 files
To clean up when I log out, I added the lines that follow to the C shell
The Bourne shell version is similar, but it needs a couple of tricks
to work on some shells; it's on the CD-ROM.
# CLEAN FILES (IF ANY) OUT OF /tmp:
set tmpf="\`ls -d $tf-* $TF-* |& grep -v ' not found'\`"
if ( "$tmpf" =~ ?* ) then
echo; echo "Your files in /tmp:"
ls -d $tmpf
echo -n "'rm -rf' them? [ny](n) "
if ( "$<" =~ y* ) rm -rf $tmpf
If I made any temporary files from my login shell or any subshells,
I get this message when I log out:
Your files in /tmp:
/tmp/jp2345-1 /tmp/jp2345-2 /tmp/jp2748-1 /tmp/09:23:45-1
'rm -rf' them?
Another way to do this is with a script like