I have problem with Bash, and I don't know why.
Under shell, I enter:
echo $$ ## print 2433
(echo $$) ## also print 2433
(./getpid) ## print 2602
Where getpid
is a C program to get current pid, like:
int main() { printf("%d", (int)getpid()); return 0; }
What confuses me is that:
- I think "(command)" is a sub-process (am i right?), and i think its pid should be different with its parent pid, but they are the same, why...
- When I use my program to show pid between parenthesis, the pid it shows is different, is it right?
- Is
$$
something like macro?
Can you help me?
getpid
would show a different process ID even if it weren't run in a subshell.echo $$ $BASHPID ; ( echo $$ $BASHPID )
demonstrates that it does. Round brackets create a subshell. The statements may change variable values, and the parent shell must not see those changes. This is implemented as afork()
operation.