Why is $$ returning the same id as the parent process?
$$
is defined to return the process ID of the parent in a subshell; from the man page under "Special Parameters":
$ Expands to the process ID of the shell. In a () subshell, it expands to the process ID of the current shell, not the subshell.
In bash
4, you can get the process ID of the child with BASHPID
.
~ $ echo $$17601~ $ ( echo $$; echo $BASHPID )1760117634
- Parentheses invoke a subshell in Bash. Since it's only a subshell it might have the same PID - depends on implementation.
- The C program you invoke is a separate process, which has its own unique PID - doesn't matter if it's in a subshell or not.
$$
is an alias in Bash to the current script PID. See differences between$$
and$BASHPID
here, and right above that the additional variable$BASH_SUBSHELL
which contains the nesting level.