Ubuntu: What is the meaning of $$1 in bash?



Question:

I am actually playing with arguments and its happened. I just thought like to see what will happen if I type $$1 and I did it and got something below.

raja@raja-BONEFISH:~$ $$1  19301: command not found  

What's that 19301 mean, is it telling something?

Bottom-Line: what does meaning for $$1 in bash.


Solution:1

$$ is the PID (process id) of the current process.

$$1 is the above PID followed by the literal string 1.

So it is telling you that your bash is the process with PID 1930.

But... free trick:

show() { eval echo \$$1; }  show PATH  /home/romano/bin:/usr/local/bin:/bin:/usr/bin  

(quite convoluted, ain't it?)

More info in TLDP.


Solution:2

  • $$ - pid of the current shell (not subshell) - see What are the special dollar sign shell variables?

  • $$1 - pid of the current shell (not subshell) followed by 1.

  • $$2 - pid of the current shell (not subshell) followed by 2.

  • $$a - pid of the current shell (not subshell) followed by a character.

  • And so on...

See the output of echo $$1.

And you get the error command not found because you are trying to execute a string composed of digits which obviously is not a command.


Solution:3

You have concatenated $$ and 1 together to get the PID of the current shell and 1, i.e. $$ stands for the PID of the currently running shell and 1 is just a character, you could do $$a, $$@ to get the PID concatenated with the following character.


Solution:4

$$ it will state 1930 which is pid of current shell .

When you type $$1 that means pid followed by one so you the output would be 19301 .

Try $$2 the output will be 19302


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »