Jonas Van Bogaert
Jonas Van Bogaert

Reputation: 339

Get PID of last executed command (NO BACKGROUND)

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.

Upvotes: 8

Views: 6004

Answers (1)

Mikhail Kartashov
Mikhail Kartashov

Reputation: 21

You can use the following construction in scripts:

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

Upvotes: 2

Related Questions