#! /bin/bash
func1()
{
echo $$
}
echo $$
( func1 )
This gives the result:
9644
9644
I expected that they would be different. Can anyone please explain why they are not?
#! /bin/bash
func1()
{
echo $$
}
echo $$
( func1 )
This gives the result:
9644
9644
I expected that they would be different. Can anyone please explain why they are not?
From the section PARAMETER EXPANSION
of man bash
:
$ Expands to the process ID of the shell. In a () subshell, it expands to the process ID of the current shell, not the subshell.
This is also mandated by the POSIX shell specification:
$ Expands to the decimal process ID of the invoked shell. In a subshell (see Shell Execution Environment ), '$' shall expand to the same value as that of the current shell.
If you want the process ID of the subshell, use $BASHPID
:
func1() { echo $BASHPID; }
echo $BASHPID
28365
( func1 )
28627