$BASHPID And $$ differ in some cases
Solution 1
An example is provided in the BASHPID
description of the bash manpage:
BASHPID
Expands to the process id of the current bash process. This
differs from $$ under certain circumstances, such as subshells
that do not require bash to be re-initialized.
Here is an example of a subshell outputting the contents of the variable, along with $$
and the contents of BASHPID
outside of the subshell.
$ echo $(echo $BASHPID $$) $$ $BASHPID
25680 16920 16920 16920
# | | | |
# | | | -- $BASHPID outside of the subshell
# | | -- $$ outside of the subshell
# | -- $$ inside of the subshell
# -- $BASHPID inside of the subshell
Solution 2
Subshells. $$
is specified by POSIX and always remains the value of the original shell process. $BASHPID
is a Bash-specific variable, and is always the value of the process from which the variable is dereferenced, counting subshells.
$ f() { printf '%s: %d, %d\n' "$1" $$ $BASHPID; };
$ ${BASH_VERSION+shopt -s lastpipe}; set +m;
$ f 1 >&2 | f 2
2: 31490, 31490
1: 31490, 32545
I did manage to convince the mksh maintainer to add BASHPID
to the most recent version, so it is somewhat portable. It is also possible to implement BASHPID
in ksh93 yourself on many platforms.
Related videos on Youtube
PersianGulf
My God is Allah and my religious book is Quran.Up to now my life was based on my God's will and whatever I gained or lost is due to my God's want. My aspiration is to put my life in favour of my lord, Allah since I believe this would be a desirable life. I am a fan and supporter of Free Software and i do all my computer works based on Freesoftware. My other interests are Linux/GNU family and BSD. My main speciality is network administration but I also have decent knowledge in C/C++ and Python progmming. In general I love programming languages. My favorite sport is mountain climbing because I love outdoors and generaly I'm a nature lover. I truely love to increase my knowledge because I'm eager to know as much as possible. Not only I have this desire for myself, but also for every human being and I will do my best to help anyone who wishes to be on this path. (You can find everythinf about me at http://pahlevanzadeh.net)
Updated on September 18, 2022Comments
-
PersianGulf over 1 year
I'm reading "BASH pocket guide of Oreilly". It said:
The process ID of the current Bash process. In some cases, this can differ from $$.
Above explanation , explained
$BASHPID
variable.Question: which cases?
-
mbaljeetsingh over 6 yearsIt should be noted that
$BASHPID
is new to BASH 4. If you are using BASH 3.x, you have to use$$
-
-
The Quark over 2 yearsWhy using
${BASH_VERSION+shopt -s lastpipe}
and not more simplyshopt -s lastpipe
? -
ormaaj over 2 years@TheQuark Only bash has
shopt
, and only bash (and ksh93v-) have configurable "lastpipe" behavior, though their defaults are backwards.${var+}
isn't a particularly good mechanism either because it's being field split. It works alright for string literals with a knownIFS
when used at the top of scripts that aren't being sourced by other scripts.