我在使用 Bash 时遇到问题,我不知道为什么。
在shell下,我输入:
echo $$ ## print 2433
(echo $$) ## also print 2433
(./getpid) ## print 2602
其中getpid
是一个获取当前pid的C程序,如:
int main() { printf("%d", (int)getpid()); return 0; }
让我困惑的是:
$$
是不是类似于宏?你能帮帮我吗?
最佳答案
$$
定义为在子shell中返回父进程ID;来自“特殊参数”下的手册页:
$ Expands to the process ID of the shell. In a () subshell, it expands to the process ID of the current shell, not the subshell.
在bash
4中,可以通过BASHPID
获取子进程ID。
~ $ echo $$
17601
~ $ ( echo $$; echo $BASHPID )
17601
17634
https://stackoverflow.com/questions/21063765/