Suppose I input the following in a shell
(while true; do echo hahaha; sleep 1; done)&
Then I know I can kill it by
fg;
(while true; do echo hahaha; sleep 1; done)& RUNNING_PID=$! kill ${RUNNING_PID}
$! will pick up the PID of the process that is running so you can do with it as you wish