Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Emulating BASHPID on bash 3.x

226 views
Skip to first unread message

arwild01

unread,
May 13, 2011, 1:32:27 PM5/13/11
to
I suspect this is a useless endeavor, but I'm stuck on bash 3.2.25 and
my employer won't upgrade. I found myself needing to try and
determine the PID of a subshell... and realizing that BASHPID variable
wasn't availble, I thought to try the following:

[arwild1@hplcsl027 ~]$ echo $$; ( set -x; eval echo \$$ )
7856
+ eval echo '$$'
++ echo 7856
7856

Needless to say I was surprised by the result. I had assumed that:

( echo $$; )

wouldn't work because the parent process (in this case, my interactive
shell) would do the variable substituation before running the
command... which is what led me to using eval and quoting... and based
on the set -x output... the eval does appear to be happening in the
subshell... but the lookup is returning an arguably incorrect value
(one which, in the case, would actually be the PPID).

Based on my reading of the bash man page... each subshell should have
its own command execution environment (it certainly has its own
process and pid )... and as such I would think my trick should work...
but it would appear that bash may taking some shortcuts here.

Could this be seen as a bug or is there something I'm overlooking? Is
there any other way to achieve what I want to do on bash 3.x ?

Thanks,

-Alan

raiz...@gmail.com

unread,
Nov 29, 2012, 2:36:29 PM11/29/12
to
I don't known if its a bug but i could emulate de BASHPID with this 'cut -d " " -f 4 /proc/self/stat'

Try this and you will see:

echo -n "$BASHPID "; cut -d " " -f 4 /proc/self/stat; ( echo -n "$BASHPID "; cut -d " " -f 4 /proc/self/stat; )
0 new messages