I would like a shell script like this

my-app &
echo $my-app-pid

But i do not know how the get the pid of the just executed command.

I know I can just use the jobs -p my-app command to grep the pid. If i want to execute the shell multiple times this method will not work Because the jobspec is ambiguous.

Best Answer

The PID of the last executed command is in the $! shell variable.

my-app &
echo $!