bash 为什么 $$ 返回与父进程相同的 ID?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21063765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 00:36:40  来源:igfitidea点击:

Why is $$ returning the same id as the parent process?

bashshellpidsubshell

提问by ruanhao

I have problem with Bash, and I don't know why.
Under shell, I enter:

我对 Bash 有问题,我不知道为什么。
在 shell 下,我输入:

echo $$    ## print 2433
(echo $$)  ## also print 2433
(./getpid) ## print 2602

"getpid" is a C program to get current pid, like:

“getpid”是一个获取当前pid的C程序,例如:

   int main() {
    printf("%d", (int)getpid());
    return 0;
   }
   int main() {
    printf("%d", (int)getpid());
    return 0;
   }

What confuses me is that:

让我困惑的是:

  1. 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...
  2. when i use my program to show pid between parenthesis, the pid it shows is different, is it right?
  3. is '$$' something like macro?
  1. 我认为“(命令)”是一个子进程(对吗?),我认为它的 pid 应该与其父 pid 不同,但它们是相同的,为什么...
  2. 当我使用我的程序在括号之间显示pid时,它显示的pid不同,对吗?
  3. '$$' 类似于宏吗?

Can you help me?

你能帮助我吗?

回答by chepner

$$is defined to return the process ID of the parent in a subshell; from the man page under "Special Parameters":

$$被定义为在子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.

$ 扩展为 shell 的进程 ID。在 () 子shell 中,它扩展为当前shell 的进程ID,而不是子shell。

In bash4, you can get the process ID of the child with BASHPID.

bash4,你可以得到与孩子的进程ID BASHPID

~ $ echo $$
17601
~ $ ( echo $$; echo $BASHPID )
17601
17634

回答by craken

You can use one of the following.

您可以使用以下方法之一。

  • $!is the PID of the last backgrounded process.
  • kill -0 $PIDchecks whether it's still running.
  • $$is the PID of the current shell.
  • $!是最后一个后台进程的PID。
  • kill -0 $PID检查它是否仍在运行。
  • $$是当前 shell 的 PID。

回答by Niels Keurentjes

  1. Parentheses invoke a subshell in Bash. Since it's only a subshell it might have the same PID - depends on implementation.
  2. 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.
  3. $$is an alias in Bash to the current script PID. See differences between $$and $BASHPIDhere, and right above that the additional variable $BASH_SUBSHELLwhich contains the nesting level.
  1. 括号调用Bash 中子shell。由于它只是一个子外壳,它可能具有相同的 PID - 取决于实现。
  2. 您调用的 C 程序是一个单独的进程,它有自己唯一的 PID - 无论它是否在子 shell 中都无关紧要。
  3. $$是 Bash 中当前脚本 PID的别名。在这里$$$BASHPID查看之间的差异,$BASH_SUBSHELL包含嵌套级别的附加变量的正上方查看。

回答by Alex

Try getppid()if you want your C program to print your shell's PID.

尝试getppid()是否希望 C 程序打印 shell 的 PID。

回答by Don-Pierre Halfaway

If you were asking how to get the PID of a known command it would resemble something like this:

如果您询问如何获取已知命令的 PID,它将类似于以下内容:

If you had issued the command below #The command issued was ***

如果你发出了下面的命令#发出的命令是***

dd if=/dev/diskx of=/dev/disky

dd if=/dev/diskx of=/dev/disky



Then you would use:

然后你会使用:

PIDs=$(ps | grep dd | grep if | cut -b 1-5)

What happens here is it pipes all needed unique characters to a field and that field can be echoed using

这里发生的事情是它将所有需要的唯一字符输送到一个字段,并且该字段可以使用

echo $PIDs

回声 $PIDs

回答by ARTEM LAPKIN

this one univesal way to get correct pid

这是获得正确 pid 的一种通用方法

pid=$(cut -d' ' -f4 < /proc/self/stat)

pid=$(cut -d' ' -f4 < /proc/self/stat)

same nice worked for sub

对 sub 也很好用

SUB(){
    pid=$(cut -d' ' -f4 < /proc/self/stat)
    echo "$$ != $pid"
}

echo "pid = $$"

(SUB)

check output

检查输出

pid = 8099
8099 != 8100