gpt4 book ai didi

linux - bash: child 无法正确获取其 PID

转载 作者:太空狗 更新时间:2023-10-29 11:05:57 26 4
gpt4 key购买 nike

为什么当我运行下面的脚本时, parent 和 child 都认为他们有相同的 pid?

#!/bin/bash

foo ()
{
while true
do
sleep 5
echo child: I am $$
done
}


( foo ) &

echo parent: I am $$ and child is $!

>./test.sh
parent: I am 26542 and child is 26543
>child: I am 26542
child: I am 26542

最佳答案

在 Bash 中,有一些 $$$BASHPID 变量,有些令人困惑。 $$ 是脚本本身的进程 ID。 $BASHPID 是当前 Bash 实例的进程 ID。这些东西不一样,但往往给出相同的结果。在你的情况下,你使用不正确。在函数 foo 中将 $$ 替换为 $BASHPID 即可解决问题。

参见 Internal VariablesBash Advanced Scripting Guide了解更多详情。

关于linux - bash: child 无法正确获取其 PID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5862421/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com