获取上次执行命令的PID(无背景)

时间:2017-08-09 09:02:40

标签: linux bash shell pid

我想知道最后执行的命令的PID。 我看了很多:

$ command &
$ pid=$!

但我在没有在后台运行命令的情况下搜索同样的事情。

1 个答案:

答案 0 :(得分:0)

您可以在脚本中使用以下构造:

PID=`sh -c "echo $$; exec your_command -with-arguments"`
echo $PID