【发布时间】:2014-11-02 02:47:19
【问题描述】:
我正在尝试获取多个 bash 进程的进程 ID:
./test_run.sh 你好 回声$$ ./test_run.sh 你好 回声$$ ./test_run.sh hello22 回声$$test_run.sh 的定义
回声“运行$ 1”但它们都返回相同的进程 ID,为什么?
【问题讨论】:
标签: bash
我正在尝试获取多个 bash 进程的进程 ID:
./test_run.sh 你好 回声$$ ./test_run.sh 你好 回声$$ ./test_run.sh hello22 回声$$test_run.sh 的定义
回声“运行$ 1”但它们都返回相同的进程 ID,为什么?
【问题讨论】:
标签: bash
$$ 返回当前 shell 进程的 PID。要获取刚刚启动的后台进程的 PID,请使用$!。
./test_run.sh hello &
echo $!
./test_run.sh hello &
echo $!
【讨论】:
test_run.sh。这对这个问题有什么影响?
$! 仅用于后台进程。为什么您期望$$ 返回不同的 PID?运行命令时,原始 shell 的 PID 不会改变。该命令在子进程中运行,但原始 shell 进程保持不变。