On Wed, Apr 30, 2014 at 04:39:28PM -0400, Chet Ramey wrote:
> On 4/30/14, 4:34 PM, Viktor Szépe wrote:
> > echo first $$ >&2 | bash -c 'echo second $$'
> > first 30551
> > second 28057
> >
> > but
> >
> > eval 'echo first $$' >&2 | eval 'echo second $$'
> > first 30551
> > second 30551
>
> $$ doesn't change in a subshell environment. It changes in your first
> example because you're starting a new shell instance.
In bash 4.0 and higher, you can use the BASHPID variable to get the process
ID of a subshell. $$ always gives the process ID of the main shell.
echo "main $$ $(echo "subshell $BASHPID")"