0

I wrote a simple c application and test on centos 6.5, the code as below

int main(int argc, char *argv[]) {
    fprintf(stderr, "%s\n", argv[1]);
    return 0;
}

when I run the app with ./test $, then the print value is $, but if I run with ./test $$, the print value changed to 119688, is any special meaning of $ in linux, I found it works correctly on windows.

pynexj
  • 19,215
  • 5
  • 38
  • 56
Yigang Wu
  • 3,596
  • 5
  • 40
  • 54

1 Answers1

2

$$ is the current process' ID:

[mhawke@localhost-localdomain ~]$ echo $$
10062
[mhawke@localhost-localdomain ~]$ ps -ef | grep bash
mhawke   10062 10056  0 10:48 pts/0    00:00:00 bash

When you run your program from the command line the shell is substituting the $$ for the process ID and your program receives that as its first argument.

To pass $$ to your program simply wrap it in single quotes:

[mhawke@localhost-localdomain ~]$ ./test '$$'

From the bash man page:

BASHPID

Expands to the process id of the current bash process. This differs from $$ under certain circumstances, such as subshells that do not require bash to be re-initialized.

mhawke
  • 84,695
  • 9
  • 117
  • 138