LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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, 08:27 AM   #1
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 707

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 08:29 AM.
 
Old 11-20-2009, 09:55 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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, 10:18 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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, 01:54 AM   #4
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 707

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, 03:03 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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, 04:19 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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, 05:35 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

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


Reply



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-03-2009 11:58 PM
ls | echo, I got blank, why can't echo take the 2nd seat in a pipeline? elinuxqs Linux - Newbie 6 11-24-2006 08:25 AM
BASH: How to NOT echo to screen with "if echo $x | grep ".*"; then" eur0dad Programming 9 07-27-2006 02:14 PM
Kphone echo (echo echo) scabies Linux - Software 0 10-18-2004 02:59 PM
Echo /devPrinting doesn't work, echo /usb/lp0 works, Testpage works, Printing doesn't Hegemon Linux - General 3 08-15-2002 01:13 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:15 AM.

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
Open Source Consulting | Domain Registration