LFCS · Question #2
When the command echo $$ outputs 12942, what is the meaning of 12942?
The correct answer is B. It is the process ID of the current shell. The special shell variable $$ expands to the process ID (PID) of the shell instance that is currently executing the command.
Question
Options
- AIt is the process ID of the echo command.
- BIt is the process ID of the current shell.
- CIt is the process ID of the last command executed.
- DIt is the process ID of the last command which has been placed in the background.
How the community answered
(20 responses)- B95% (19)
- C5% (1)
Why each option
The special shell variable `$$` expands to the process ID (PID) of the shell instance that is currently executing the command.
The `$$` variable does not represent the process ID of the `echo` command itself; `echo` runs as a child process and would have a different PID.
The `$$` special parameter in shell scripting is used to retrieve the process ID of the current shell or subshell in which the command is running. Thus, `echo $$` displays the PID of your interactive shell session.
The `$$` variable specifically refers to the current shell's PID, not the PID of the last command executed, which would typically be a different value.
The `$$` variable refers to the current shell's PID, while the PID of the last command placed in the background is stored in the `$!` variable.
Concept tested: Shell special variable `$$` (current shell PID)
Source: https://www.gnu.org/software/bash/manual/bash.html#Special-Parameters
Topics
Community Discussion
No community discussion yet for this question.