Process Management
Background and Wait
Collecting Status
& starts a command in the background. $! records its process id, and wait collects the final exit status.
Program
Play the script to start a short background job and collect its selected status.
background_wait.sh
#!/usr/bin/env bash
job_status=
(exit "$job_status") &
pid=$!
wait "$pid"
status=$?
echo "status=$status"
#!/usr/bin/env bash
job_status=
(exit "$job_status") &
pid=$!
wait "$pid"
status=$?
echo "status=$status"
#!/usr/bin/env bash
job_status=
(exit "$job_status") &
pid=$!
wait "$pid"
status=$?
echo "status=$status"
background job
`command &` starts a command and immediately returns control to the script.
$!
`$!` stores the process id of the most recent background command.
wait
`wait "$pid"` pauses until that process finishes and returns its status.