From: David W. Hodgins on
On Fri, 08 Jan 2010 08:53:08 -0500, Moody <nasir.mahmood(a)gmail.com> wrote:

> I've already tried like below
>
> $ (echo $$)
> 17317
> $ echo $$
> 17317
> both shells have same pid, which I understand as same shell and not a
> subshell..

In man bash, under "Special Parameters", it states ...

$ Expands to the process ID of the shell. In a () subshell, it expands to the
process ID of the current shell, not the subshell.

Just after that, under "Shell Variables", it has

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.

[dave(a)hodgins ~]$ echo $BASHPID
2545
[dave(a)hodgins ~]$ (echo $BASHPID)
2653

So that is running a subshell.

Regards, Dave Hodgins

--
Change nomail.afraid.org to ody.ca to reply by email.
(nomail.afraid.org has been set up specifically for
use in usenet. Feel free to use it yourself.)
From: Chris F.A. Johnson on
On 2010-01-08, David W. Hodgins wrote:
> On Fri, 08 Jan 2010 08:53:08 -0500, Moody <nasir.mahmood(a)gmail.com> wrote:
>
>> I've already tried like below
>>
>> $ (echo $$)
>> 17317
>> $ echo $$
>> 17317
>> both shells have same pid, which I understand as same shell and not a
>> subshell..
>
> In man bash, under "Special Parameters", it states ...
>
> $ Expands to the process ID of the shell. In a () subshell, it expands to the
> process ID of the current shell, not the subshell.
>
> Just after that, under "Shell Variables", it has
>
> 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.

That variable is new to bash4.

> [dave(a)hodgins ~]$ echo $BASHPID
> 2545
> [dave(a)hodgins ~]$ (echo $BASHPID)
> 2653
>
> So that is running a subshell.


--
Chris F.A. Johnson, author <http://shell.cfajohnson.com/>
===================================================================
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)
===== My code in this post, if any, assumes the POSIX locale =====
===== and is released under the GNU General Public Licence =====
From: Ben Bacarisse on
Jon LaBadie <jlabadie(a)aXcXm.org> writes:
<snip>
> As grep can do the counting also, this can be simplified to:
>
> counter=$(ps -ef | grep -c "[s]endmail")
<snip>
> If your system has pgrep, you could also use:
>
> counter=$(pgrep "the_process" | wc -l)

The pgrep I have also has -c giving just

counter=$(pgrep -c "the_process")

--
Ben.
From: Mark Hobley on
Moody <nasir.mahmood(a)gmail.com> wrote:
>
> `man sh `
>
>
> (list)
> Execute list in a sub-shell.
>
>
> I've already tried like below
>
> $ (echo $$)
> 17317
> $ echo $$
> 17317
> $
>
>
> both shells have same pid, which I understand as same shell and not a
> subshell..

Yeah. I encountered a similar thing last year. The documentation in the manual
page may not be correct. It depends on the shell:

Barry Margolin <barmar(a)alum.mit.edu> wrote:
> Parentheses are standard shell syntax, they cause the command to be run
> in a subshell.

Hmmm, I just tried this with a script as follows:

#!/bin/sh
echo "`ps -fL`"
echo
echo "`(ps -fL)`"
echo

UID PID PPID LWP C NLWP STIME TTY TIME CMD
mark 1071 1070 1071 0 1 Apr11 pts/2 00:00:00 sh
mark 2773 1071 2773 0 1 06:38 pts/2 00:00:00 sh test
mark 2774 2773 2774 0 1 06:38 pts/2 00:00:00 ps -fL

UID PID PPID LWP C NLWP STIME TTY TIME CMD
mark 1071 1070 1071 0 1 Apr11 pts/2 00:00:00 sh
mark 2773 1071 2773 0 1 06:38 pts/2 00:00:00 sh test
mark 2775 2773 2775 0 1 06:38 pts/2 00:00:00 ps -fL

I would have expected an extra shell in the second set and the
second ps to have a different parent, because it runs from a subshell.

I try various shells:

The Heirloom shell ...

hsh test.sh
UID PID PPID LWP C NLWP STIME TTY TIME CMD
mark 7517 7515 7517 0 1 10:10 pts/0 00:00:00 -sh
mark 7525 7517 7525 0 1 10:11 pts/0 00:00:00 hsh test.sh
mark 7526 7525 7526 0 1 10:11 pts/0 00:00:00 ps -fL

UID PID PPID LWP C NLWP STIME TTY TIME CMD
mark 7517 7515 7517 0 1 10:10 pts/0 00:00:00 -sh
mark 7525 7517 7525 0 1 10:11 pts/0 00:00:00 hsh test.sh
mark 7527 7525 7527 0 1 10:11 pts/0 00:00:00 ps -fL

(no extra process)

ash386 (0.2a) ...

ash test.sh
UID PID PPID LWP C NLWP STIME TTY TIME CMD
mark 7517 7515 7517 0 1 10:10 pts/0 00:00:00 -sh
mark 7521 7517 7521 0 1 10:11 pts/0 00:00:00 ash test.sh
mark 7522 7521 7522 0 1 10:11 pts/0 00:00:00 ps -fL

UID PID PPID LWP C NLWP STIME TTY TIME CMD
mark 7517 7515 7517 0 1 10:10 pts/0 00:00:00 -sh
mark 7521 7517 7521 0 1 10:11 pts/0 00:00:00 ash test.sh
mark 7523 7521 7523 0 1 10:11 pts/0 00:00:00 ash test.sh
mark 7524 7523 7524 0 1 10:11 pts/0 00:00:00 ps -fL

wahey ... there is that extra process

The Debian version of ash ...

UID PID PPID LWP C NLWP STIME TTY TIME CMD
mark 1611 1610 1611 0 1 Apr11 pts/5 00:00:00 sh
mark 3176 1611 3176 0 1 10:17 pts/5 00:00:00 dash test
mark 3177 3176 3177 0 1 10:17 pts/5 00:00:00 ps -fL

UID PID PPID LWP C NLWP STIME TTY TIME CMD
mark 1611 1610 1611 0 1 Apr11 pts/5 00:00:00 sh
mark 3176 1611 3176 0 1 10:17 pts/5 00:00:00 dash test
mark 3178 3176 3178 0 1 10:17 pts/5 00:00:00 ps -fL

(no extra process)

posh ...

posh test
UID PID PPID LWP C NLWP STIME TTY TIME CMD
mark 1611 1610 1611 0 1 Apr11 pts/5 00:00:00 sh
mark 3323 1611 3323 0 1 10:23 pts/5 00:00:00 posh test
mark 3324 3323 3324 0 1 10:23 pts/5 00:00:00 ps -fL

UID PID PPID LWP C NLWP STIME TTY TIME CMD
mark 1611 1610 1611 0 1 Apr11 pts/5 00:00:00 sh
mark 3323 1611 3323 0 1 10:23 pts/5 00:00:00 posh test
mark 3325 3323 3325 0 1 10:23 pts/5 00:00:00 ps -fL

(no extra process)

The korn shell:

$ ksh test.sh
UID PID PPID LWP C NLWP STIME TTY TIME CMD
mark 23833 23832 23833 0 1 23:30 pts/4 00:00:00 ksh
mark 23835 23833 23835 0 1 23:30 pts/4 00:00:00 ksh test.sh
mark 23836 23835 23836 0 1 23:30 pts/4 00:00:00 ps -fL

UID PID PPID LWP C NLWP STIME TTY TIME CMD
mark 23833 23832 23833 0 1 23:30 pts/4 00:00:00 ksh
mark 23835 23833 23835 0 1 23:30 pts/4 00:00:00 ksh test.sh
mark 23837 23835 23837 0 1 23:30 pts/4 00:00:00 ps -fL

(no extra process)

sash ...

Now this is a strange shell. I cannot execute the the script by typing:

sash test

How odd ...
Stand-alone shell (version 3.7)
Usage: sash [-a] [-q] [-f fileName] [-c command] [-p prompt] [-i]

Right then ... check this out ...
sash -c test

UID PID PPID LWP C NLWP STIME TTY TIME CMD
mark 1611 1610 1611 0 1 Apr11 pts/5 00:00:00 sh
mark 3305 1611 3305 0 1 10:20 pts/5 00:00:00 sash
mark 3318 3305 3318 0 1 10:21 pts/5 00:00:00 sash -c ./test
mark 3319 3318 3319 0 1 10:21 pts/5 00:00:00 /bin/sh ./test
mark 3320 3319 3320 0 1 10:21 pts/5 00:00:00 ps -fL

UID PID PPID LWP C NLWP STIME TTY TIME CMD
mark 1611 1610 1611 0 1 Apr11 pts/5 00:00:00 sh
mark 3305 1611 3305 0 1 10:20 pts/5 00:00:00 sash
mark 3318 3305 3318 0 1 10:21 pts/5 00:00:00 sash -c ./test
mark 3319 3318 3319 0 1 10:21 pts/5 00:00:00 /bin/sh ./test
mark 3321 3319 3321 0 1 10:21 pts/5 00:00:00 ps -fL

So the "stand alone shell" forks a different shell to run that external
command ... ha ha!

Here is another funny bit:

I create a hashbang at the beginning of the script:

#!/bin/sash
echo "`ps -fL`"
echo
echo "`(ps -fL)`"
echo

chmod +x test

Right ... what the hell is that going to do? ...

../test
Stand-alone shell (version 3.7)
Usage: sash [-a] [-q] [-f fileName] [-c command] [-p prompt] [-i]

Mark.

--
Mark Hobley
Linux User: #370818 http://markhobley.yi.org/

From: Sven Mascheck on
Moody wrote:

> $ (echo $$)
> 17317
> $ echo $$
> 17317
> $
>
> both shells have same pid, which I understand as same shell and not a
> subshell..

A subshell is "only" a new environment, which has a relationship
like a child. But it's not a new child process. In regard to
scripting this isn't necessary, but the isolated environment
is usually sufficient. For a new process you should explicitly
call a new shell or at least send the command into the background.