Get PID of last executed command (NO BACKGROUND)

Viewed 3706

I want to know the PID of the last executed command. I saw this a lot:

$ command &
$ pid=$!

But I'm searching for the same thing without running the command in the background.

1 Answers

You can use the following construction in scripts:

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