在 bash 中,你有这个方便的变量: $BASHPID ,它总是返回当前运行的子 shell 的 PID。如何在 ksh 中获取子 shell 的 PID?例如看下面的代码:
#!/usr/bin/ksh93
echo "PID at start: $$"
function run_in_background {
echo "PID in run_in_background $$"
run_something &
echo "PID of backgrounded run_something: $!"
}
function run_something {
echo "*** PID in run_something: $$"
sleep 10;
}
run_in_background
echo "PID of run in background $!"
Run Code Online (Sandbox Code Playgroud)
这将输出以下内容:
PID at start: 5328
PID in run_in_background 5328
*** PID in run_something: 5328
PID of backgrounded run_something: 5329
PID of run in background …
Run Code Online (Sandbox Code Playgroud)