Difficulty: Easy
Correct Answer:
Explanation:
Introduction / Context:
Shell scripting often needs the process ID (PID) to create unique filenames, manage lock files, or send signals. POSIX shells provide built-in special parameters that expand to useful values without invoking external commands.
Given Data / Assumptions:
Concept / Approach:
The special parameter expands to the PID of the current shell. Other related parameters include
$!
(PID of the most recently executed background pipeline), $0
(name of the shell or script), and $*
(all positional parameters).
Step-by-Step Solution:
echo
to see the PID.Use it in filenames: tmpfile=/tmp/app.
toavoidcollisions.ps−p
toavoidcollisions.ps−p
to see the matching process.Remember $!
is different (last background job PID).
Verification / Alternative check:
In an interactive shell, compare echo
withthePIDreportedbyps
foryourshellprocess.Insideasubshell,observethat
withthePIDreportedbyps
foryourshellprocess.Insideasubshell,observethat can change because it refers to that subshell's PID.
Why Other Options Are Wrong:
iscorrect.
CommonPitfalls:
Using
iscorrect.
CommonPitfalls:
Using inside subshells without realizing it changes; confusing
$!
and
;forgettingtoquotewhenembeddinginpathswithothervariables.
FinalAnswer:
;forgettingtoquotewhenembeddinginpathswithothervariables.
FinalAnswer:
Discussion & Comments