LX0-104 · 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 parameter in shell scripting expands to the process ID (PID) of the shell in which it is executed. Therefore, echo $$ displays the PID of the current shell.
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
(25 responses)- A4% (1)
- B92% (23)
- C4% (1)
Why each option
The `$$` special parameter in shell scripting expands to the process ID (PID) of the shell in which it is executed. Therefore, `echo $$` displays the PID of the current shell.
The `echo` command itself is a separate process, but `$$` does not represent its PID; rather, it represents the PID of the parent shell process.
The `$$` special parameter is a predefined shell variable that holds the process ID (PID) of the shell itself. When `echo $$` is executed, it prints the PID of the shell process that is running the `echo` command.
The `$$` special parameter refers to the current shell's PID, not the PID of the last command executed, which is typically stored in `$!` for background processes.
The `$$` special parameter does not refer to the PID of the last background command; that is stored in the `$!` variable.
Concept tested: Shell special parameters (PID)
Source: https://www.gnu.org/software/bash/manual/bash.html#Special-Parameters
Topics
Community Discussion
No community discussion yet for this question.