LFCS · Question #254
In the following command and its output, echo $$ 12942 which of the following is 12942?
The correct answer is B. The process ID of the current shell. This question tests knowledge of special Bash shell variables, specifically what $$ expands to.
Question
Options
- AThe process ID of the echo command.
- BThe process ID of the current shell.
- CThe process ID of the last command executed.
- DThe process ID of the last backgrounded command.
How the community answered
(27 responses)- B89% (24)
- C7% (2)
- D4% (1)
Why each option
This question tests knowledge of special Bash shell variables, specifically what `$$` expands to.
The process ID of the `echo` command would be a child process, not the current shell's PID, which `$$` refers to.
In Bash, `$$` is a special parameter that expands to the process ID (PID) of the current shell process. When `echo $$` is executed, the shell substitutes `$$` with its own unique process identifier, which is then printed to standard output.
The process ID of the last command executed is not represented by `$$` but can be found using other means, such as `!$`, in some contexts.
The process ID of the last backgrounded command is typically referred to using job control variables or `!$`, not `$$`.
Concept tested: Bash special variable `$$`
Source: https://www.gnu.org/software/bash/manual/bash.html#Special-Parameters
Topics
Community Discussion
No community discussion yet for this question.