LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-20-2009, 09:27 AM   #1
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 704

Rep: Reputation: 55
echo $$


Dear all....

I have a file named new.sh which has executable permission, contents of the file is
Code:
echo $$
./new.sh will give the PID of the shell in which new.sh executes....
. new.sh will be executed in current shell so it displays PID of current shell

But if i execute following on command prompt
Code:
echo $$
(echo $$)
First will display current shell's PID, its fine
Why second command will displays current shell's PID eventhough it executes in subshell ????

Last edited by vinaytp; 11-20-2009 at 09:29 AM.
 
Old 11-20-2009, 10:55 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Note #5 here says that $$ in a subshell gives the PID of the script, not the subshell.

http://www.tldp.org/LDP/abs/html/int...s.html#PROCCID
 
Old 11-20-2009, 11:18 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by David the H. View Post
Note #5 here says that $$ in a subshell gives the PID of the script, not the subshell.

http://www.tldp.org/LDP/abs/html/int...s.html#PROCCID
And so it does. My gast is flabbered
Code:
c:~$ echo $$
4128
c:~$ (eval echo '$$')
4128
 
Old 11-23-2009, 02:54 AM   #4
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 704

Original Poster
Rep: Reputation: 55
Dear all...

My doubt is not clear...

why
Code:
(echo $$ )
should give current shell's PID why not subshell's PID, as it executes in subshell
 
Old 11-23-2009, 04:03 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by vinaytp View Post
Dear all...

My doubt is not clear...

why
Code:
(echo $$ )
should give current shell's PID why not subshell's PID, as it executes in subshell
Because that is how bash is designed, as described in the link given by David the H. Surprised me.
 
Old 11-23-2009, 05:19 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
In other words, we don't know why exactly...only that the behavior is intentional. On another section of that page it says that $$ returns the PID of the parent process. Perhaps it's part of the posix specification or something.

Apparently Bash v.4 has a new $BASHPID builtin variable that can give you the subshell's PID.
 
Old 11-23-2009, 06:35 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
My ghast hasn't been so flabbered in ages
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
echo $USER/ sudo echo $USER jamescondron Programming 6 06-04-2009 12:58 AM
ls | echo, I got blank, why can't echo take the 2nd seat in a pipeline? elinuxqs Linux - Newbie 6 11-24-2006 09:25 AM
BASH: How to NOT echo to screen with "if echo $x | grep ".*"; then" eur0dad Programming 9 07-27-2006 03:14 PM
Kphone echo (echo echo) scabies Linux - Software 0 10-18-2004 03:59 PM
Echo /devPrinting doesn't work, echo /usb/lp0 works, Testpage works, Printing doesn't Hegemon Linux - General 3 08-15-2002 02:13 PM


All times are GMT -5. The time now is 08:03 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration