How to use echo $! to wait for a background process to finish


The echo $! command returns the process ID of the most recently started background process. This can be very useful when you want to wait for a process to finish before continuing with your script.

Here is an example:

echo $! returns the process id of the most recently started background process. This is very helpful when you want to wait for a process to finish.
wait $pid waits until the process with the PID $pid has finished.

./long-running-task.sh &
pid=$!
./do-other-work.sh
wait $pid