What is /tmp/t$$?

744 Views Asked by At

The title, basically. I've seen it used in some progs, like here to do something if the output of a command contains some text:

#!/bin/bash
tmp=/tmp/t$$
mycommand > $tmp 
if grep -q 'Mytext' $tmp
then
   action
fi

rm -f $tmp
exit 0

I find it very usefull, and started using it, but I don't like using thing I don't really understand. Two things are unclear for me here:

  1. what the tmp=/tmp/t$$ exactly does (what is that t$$ thing?? The double dollar really confuse me)
  2. How does the $tmp works? I mean, I get that we can put the output of the command in $tmp just as if we were writing it to a file (and find it better with $tmp, without creating temporary files), but when we delete it with rm $tmp, what exactly happens? Is there a temporary file named t$$in /tmp that got deleted?

I couldn't find an answer online and I really would like to understand it.

1

There are 1 best solutions below

0
Scotronix On

The double-dollar is the process-id of the current shell.

The script above sets the variable "tmp" to the "/tmp/t123456" (where 123456 is the process id of the current running script).

I think the "t" is just a string the author chose to use.

Another approach could be to do something like:

#!/bin/bash
temp_file=/tmp/t$RANDOM
mycommand > $temp_file 

...

rm -f $temp_file
exit 0

-Scott