56

there is a script I evolved with it, it has line of command like below :

mytemp=`echo ${sourcedir}|awk -F/ '{printf "/%s/tmp",$2}'`/`basename $0`-$1.$$

at the last of the command we see $$ that produces a number. when I use echo $$ in bash I also see a number like bellow:

 #echo $$
 23019

what exactly is this number, and what is $$?

amir jj
  • 1,212

2 Answers2

64

From Advanced Bash-Scripting Guide:

$$ is the process ID (PID) of the script itself.

$BASHPID is the process ID of the current instance of Bash. This is not the same as the $$ variable, but it often gives the same result.

Vombat
  • 12,884
18

$$ is the process ID of the current shell instance. So in your case the number, 23019, is the PID of that instance of bash.

The following should give you a better idea:

ps -p $$
heemayl
  • 56,300
  • It often finds use in scripts to construct "unique" temp file names. – chaos Jun 23 '16 at 08:48
  • 12
    @chaos Those scripts should use mktemp. – Kusalananda Jun 23 '16 at 08:49
  • Thanks @Kusalananda. tmp_file=\mktemp /tmp/my_file_XXX` ; trap 'rm -f $tmp_file' EXIT` is indeed safer (making sure $tmp_file is deleted at shell exit) – Noam Manos Jul 06 '20 at 14:31
  • @NoamManos: even better if mktemp is used without the /tmp/my_file_XXX argument, and let the system choose an appropriate path (which might not even be in /tmp) – MestreLion Jul 20 '22 at 10:14