Which of the following would return the process ID of the sleep command?
- sleep 1 & echo $!
- sleep 1 & echo $?
- sleep 1 & echo $#
- sleep 1 & echo $$
The $! special variable in Unix/Linux shells contains the process ID (PID) of the most recently backgrounded command. When you run 'sleep 1 &', it starts in the background, and $! captures its PID. $$ is the current shell's PID, $? is exit status, $# is argument count.
sleep 1 & echo $! is correct. In bash, $! expands to the PID of the most recently backgrounded job — exactly the process ID of the sleep 1 command just launched in the background. $? gives the exit status of the last foreground command, $# gives the number of positional parameters passed to the script, and $$ gives the PID of the current shell itself, not of the backgrounded child. Only $! is defined to track background job PIDs.