59

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 $$?

2
  • 1
    It's your current shell pid
    – cuonglm
    Commented Jun 23, 2016 at 8:47
  • 1
    That question is not completely right about the answer.
    – amir jj
    Commented Jun 23, 2016 at 11:23

2 Answers 2

68

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.

19

$$ 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 $$
4
  • It often finds use in scripts to construct "unique" temp file names.
    – chaos
    Commented Jun 23, 2016 at 8:48
  • 12
    @chaos Those scripts should use mktemp.
    – Kusalananda
    Commented Jun 23, 2016 at 8: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
    Commented Jul 6, 2020 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
    Commented Jul 20, 2022 at 10:14

Not the answer you're looking for? Browse other questions tagged .