【发布时间】:2014-01-10 15:10:25
【问题描述】:
我正在尝试获取当前正在执行的子 shell 的 pid - 但 $$ 只返回父 pid:
#!/usr/bin/sh
x() {
echo "I am a subshell x echo 1 and my pid is $$"
}
y() {
echo "I am a subshell y echo 1 and my pid is $$"
}
echo "I am the parent shell and my pid is $$"
x &
echo "Just launched x and the pid is $! "
y &
echo "Just launched y and the pid is $! "
wait
输出
I am the parent shell and my pid is 3107
Just launched x and the pid is 3108
I am a subshell x echo 1 and my pid is 3107
Just launched y and the pid is 3109
I am a subshell y echo 1 and my pid is 3107
正如您在上面看到的,当我从后台运行的函数中运行 $$ 时,它不会像从父 shell 中运行 $! 时那样显示 PID。
【问题讨论】: