Why does running echo $$ in bash return a number like 7190, while running echo $ only returns a $?

  • If you use set -x to enable tracing of commands, you'll see the shell expands $$ before running echo on it. This is your clue to go look for $$ in the bash man page. (set +x to disable tracing again). – Peter Cordes Aug 8 '17 at 0:22
  • 1
    If you want to echo a literal $$, quote the args to echo: echo 'foo $$ bar' prints exactly that, with multiple spaces. – Peter Cordes Aug 8 '17 at 0:23
  • 1
    VTR - Looking at the close duplicate there is no reference in the question to "$$" nor is it in any of the answers. Using this close as duplicate logic we could close every question in AU that has a $ in it which is probably thousands. – WinEunuuchs2Unix Aug 8 '17 at 2:24
  • @WinEunuuchs2Unix There a number of special variables in bash. There's no point to having a question on each of them. And it is in the answers. – muru Aug 8 '17 at 5:32
  • 1
    The part of this question that interests me most is why "running echo $ returns $"--that is, why is $ by itself is treated literally, rather than being considered a bad substitution like ${}? ($ by itself can't be expanded, and is treated literally; ${} can't be expanded, and is an error.) If this is reopened, perhaps additional answers will be posted to explain that. I can sort of address this--$x is actually a short form of ${x} where x is a valid parameter name--but I suspect others might be able to explain it from a more historical, formal, or practical perspective. – Eliah Kagan Aug 8 '17 at 5:44


$$: Expands to the process ID of the shell. In a () subshell, it expands to the process ID of the invoking shell, not the subshell (see the link to the manual below).

rinzwind@schijfwereld:~$ echo $$
rinzwind@schijfwereld:~$ ps -ef |grep 3244
rinzwind  3244  3237  0 19:06 pts/0    00:00:00 /bin/bash

Very useful when coding software. And it can be used as a crude (mktemp would be the better method) way of creating temp files

1 $ has no special meaning so it gives you what echo always does: return it.

There is a manual page dedicated to this (3.4.2 Special Parameters).

  • Why does echo $$$$ return a different number? – NerdOfLinux Aug 7 '17 at 17:11
  • 6
    @NerdOfLinux it's returning the same number twice – Ravexina Aug 7 '17 at 17:11
  • 4
    @NerdOfLinux it does not? It shows the same number 2 times – Rinzwind Aug 7 '17 at 17:12
  • 4
    @MontyHarder: Predictable names (like /tmp/myscript.$$) for tmp files are usually a security vulnerability. See linuxsecurity.com/content/view/115462/151 for why mktemp is the right way, if you can't avoid needing a tmp file in the first place. Including the PID of your script in the mktemp template is a good idea, though, for the reason you point out. – Peter Cordes Aug 8 '17 at 0:18
  • 1
    "Very useful when coding software: ps -ef | grep $$." -- This is going to give false positives rather frequently. – hvd Aug 8 '17 at 17:21

In bash to state using a variable we use $, using $$ with the first dollar sign we are saying that I want to use a variable and using the second one we are telling that the name of that variable is actually a $. it's actually a naming convention, and this variable contains the process id of the current shell.

As you asked in the comments with $$$$ you are returning the same process id twice.

There are other variables too (From here):

  • $1, $2, $3, ... are the positional parameters.
  • "$@" is an array-like construct of all positional parameters, {$1, $2, $3 ...}.
  • "$*" is the IFS expansion of all positional parameters, $1 $2 $3 ....
  • $# is the number of positional parameters.
  • $- current options set for the shell.
  • $$ pid of the current shell (not subshell).
  • $_ most recent parameter (or the abs path of the command to start the current shell immediately after startup).
  • $IFS is the (input) field separator.
  • $? is the most recent foreground pipeline exit status.
  • $! is the PID of the most recent background command.
  • $0 is the name of the shell or shell script.
  • 1
    Don't tell Windy but this time I'm up-voting your answer which I think covers a wide range of special characters. I think I'll get a sportsmanship badge down the line but, greed aside, I really do like your all encompassing succinct answer. You might want to post this same answer on: Special variables in bash – WinEunuuchs2Unix Aug 8 '17 at 2:39
  • @WinEunuuchs2Unix I wrote an alternative there, not sure if its add anything helpful to that question, anyway ... ;) – Ravexina Aug 8 '17 at 6:06

Here is a real life application of $$ taken from Lock Screen Timer:

# Check if lock screen timer already running
pID=$(pgrep -f "${0##*/}") # All PIDs matching lock-screen-timer name
PREVIOUS=$(echo "$pID" | grep -v ^"$$") # Strip out this running copy ($$$)
if [ "$PREVIOUS" != "" ]; then
    kill "$PREVIOUS"
    rm ~/.lock-screen-timer-remaining
    zenity --info --title="Lock screen timer already running" --text="Previous lock screen timer has been terminated."

In this code snippet the line:

PREVIOUS=$(echo "$pID" | grep -v ^"$$") # Strip out this running copy 

uses the current running process ($$) to remove it (denoted by not -v) from the list of all processes running under the same name (lock-screen-timer in this case).

If there was a previous running copy the code kills it and delete the work file it was using.

  • In grep -v ^"$$", ^ doesn't mean "not"--v does. -v causes grep to display non-matching lines instead of matching ones. In a regular expression, ^ only means "not" when it appears immediately following the opening [ of a character class. For example, [^ac] matches any character besides a or c. Outside character classes, ^ is an anchor that matches the empty string at the beginning of a line. For example, ^x matches an x that appears at the beginning of a line. – Eliah Kagan Aug 8 '17 at 0:47
  • @EliahKagan Thank you for correcting me. I've changed the parameter to -v. The ^ was probably necessary because a Desktop link to the script had a modified version of the program name. But for the life of me I honestly can't remember the exact reason now. It was sooo long ago I last debugged the script... Anyway thanks again for your experienced insight. – WinEunuuchs2Unix Aug 8 '17 at 1:11
  • I think you were right to include ^. If the current PID is 2345, grep -v "$$" will drop 2345 as desired but also drop 12345. With grep -v ^"$$", only PIDs that really start with 2345 are dropped. But it occurs to me that PIDs that start like the current one but have extra digits are still dropped. Assuming that's unintended, you can use grep -v ^"$$"$. ($ is the end-of-line anchor. You may want to quote it but--as the OP here observed--you don't have to.) Or take advantage of -x and use grep -vx "$$" or, as it would no longer need to be treated as a regex, grep -Fvx "$$". – Eliah Kagan Aug 8 '17 at 1:48
  • Ahh three $ is what I had in the initial version but Serg and another fellow coerced me to take it out and revert to two $ I'll have to revisit the whole issue next week. It seems like it was simply the right$ on the wrong side of the tracks ". Thanks again for your wonderful insights. – WinEunuuchs2Unix Aug 8 '17 at 1:51
  • @EliahKagan It appears I was wrong looking back and what Serg and someone else said regarding "$$" vs. "$$$". The "$$$" solution had broken my script so I reverted back to "$$". I will try out your "$$"$ suggestion which is probably what they intended to post in the aforementioned link. – WinEunuuchs2Unix Aug 8 '17 at 2:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.