How can I get the pid of a subshell?Do parentheses really put the command in a subshell?How can I detect if...

Understanding the reasoning of the woman who agreed with Shlomo to "cut the baby in half"

Is it illegal to withhold someone's passport and green card in California?

Android Material and appcompat Manifest merger failed in react-native or ExpoKit

What is the meaning of "понаехать"?

Walk a Crooked Path

How many people are necessary to maintain modern civilisation?

Can Ogre clerics use Purify Food and Drink on humanoid characters?

How to remove this component from PCB

Should I submit Original or copy of my passport to canadian embassy?

How to make clear to people I don't want to answer their "Where are you from?" question?

Why do textbooks often include the solutions to odd or even numbered problems but not both?

How would modern naval warfare have to have developed differently for battleships to still be relevant in the 21st century?

Do I need a shock-proof watch for cycling?

What can I do with a research project that is my university’s intellectual property?

UK - Working without a contract. I resign and guy wants to sue me

Can a rogue use Sneak Attack in a Darkness spell cast by another player?

Shooting someone's past self using special relativity

How did Gollum enter Moria?

How do I professionally let my manager know I'll quit over an issue?

Trainee keeps passing deadlines for independent learning

Is there any difference between Т34ВМ1 and КМ1858ВМ1/3?

CircuiTikZ: Start ground relative to the closest component

Helping ease my back pain by studying 13 hours everyday , even weekends

What's currently blocking the construction of the wall between Mexico and the US?



How can I get the pid of a subshell?


Do parentheses really put the command in a subshell?How can I detect if I'm in a subshell?$BASHPID And $$ differ in some casesBash subshell creation with curly bracesHow to get the pid to the last background appWhy is subshell created by background control operator (&) not displayed under pstreeHow can we prevent parameter expansion?Subshell does not create a separate processGet PID and return code from 1 line bash callSupress errors in a subshell?Why do `jobs` and `dirs` run in command subsitution, process substitution, pipeline, and background jobs output the same as in original shell?Why does bash not remove backslash in the quote removal step in this example?How can I stop a child process of a subshell (as per SIGSTOP) before the subshell exits?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







7















How can I get the pid of a subshell?



For example:



$ echo $$
16808


This doesn't work, because the original shell expands $$:



$ ( echo $$ )
16808


Why does single quoting not work? After the original shell removes the single quote, does the subshell not expand $$ in itself?



$ ( echo '$$' )
$$


Why does eval not work either? Is eval run by the subshell? Why does it give me the original shell's PID?



$ ( eval echo '$$' )
16808


Thanks.










share|improve this question

























  • I suggest a reopen, because the questions are essentially different in my opinion ("how to avoid $$ expansion" vs. "different pid in subshell").

    – peterh
    2 hours ago




















7















How can I get the pid of a subshell?



For example:



$ echo $$
16808


This doesn't work, because the original shell expands $$:



$ ( echo $$ )
16808


Why does single quoting not work? After the original shell removes the single quote, does the subshell not expand $$ in itself?



$ ( echo '$$' )
$$


Why does eval not work either? Is eval run by the subshell? Why does it give me the original shell's PID?



$ ( eval echo '$$' )
16808


Thanks.










share|improve this question

























  • I suggest a reopen, because the questions are essentially different in my opinion ("how to avoid $$ expansion" vs. "different pid in subshell").

    – peterh
    2 hours ago
















7












7








7


1






How can I get the pid of a subshell?



For example:



$ echo $$
16808


This doesn't work, because the original shell expands $$:



$ ( echo $$ )
16808


Why does single quoting not work? After the original shell removes the single quote, does the subshell not expand $$ in itself?



$ ( echo '$$' )
$$


Why does eval not work either? Is eval run by the subshell? Why does it give me the original shell's PID?



$ ( eval echo '$$' )
16808


Thanks.










share|improve this question
















How can I get the pid of a subshell?



For example:



$ echo $$
16808


This doesn't work, because the original shell expands $$:



$ ( echo $$ )
16808


Why does single quoting not work? After the original shell removes the single quote, does the subshell not expand $$ in itself?



$ ( echo '$$' )
$$


Why does eval not work either? Is eval run by the subshell? Why does it give me the original shell's PID?



$ ( eval echo '$$' )
16808


Thanks.







bash process subshell






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 27 '18 at 14:59









Kusalananda

151k18298478




151k18298478










asked Nov 27 '18 at 13:23









TimTim

29.6k85279515




29.6k85279515













  • I suggest a reopen, because the questions are essentially different in my opinion ("how to avoid $$ expansion" vs. "different pid in subshell").

    – peterh
    2 hours ago





















  • I suggest a reopen, because the questions are essentially different in my opinion ("how to avoid $$ expansion" vs. "different pid in subshell").

    – peterh
    2 hours ago



















I suggest a reopen, because the questions are essentially different in my opinion ("how to avoid $$ expansion" vs. "different pid in subshell").

– peterh
2 hours ago







I suggest a reopen, because the questions are essentially different in my opinion ("how to avoid $$ expansion" vs. "different pid in subshell").

– peterh
2 hours ago












2 Answers
2






active

oldest

votes


















7














In addition to bash's $BASHPID, you can do it portably with:



pid=$(exec sh -c 'echo "$PPID"')


Example:



(pid=$(exec sh -c 'echo "$PPID"'); echo "$$ $pid")


You can make it into a function:



# usage getpid [varname]
getpid(){
pid=$(exec sh -c 'echo "$PPID"')
test "$1" && eval "$1=$pid"
}


Notice that some shells (eg. zsh or ksh93) do NOT start a subprocess for each subshell created with (...); in that case, $pid may be end up being the same as $$, which is just right, because that's the PID of the process getpid was called from.






share|improve this answer


























  • Thanks. By portably, you mean ...?

    – Tim
    Nov 27 '18 at 15:13











  • Should work in any POSIX shell -- eg in debian's /bin/sh (dash) and in busybox (ash).

    – mosvy
    Nov 27 '18 at 15:15






  • 1





    No. But please do not assume that a subshell is necessarily run in a subprocess -- that is not the case in ksh93, for instance.

    – mosvy
    Nov 27 '18 at 15:18








  • 1





    It will work fine in ksh93 -- it will always return the pid of the process it was called from. It's the (...) from the example which may not spawn a separate process, as it does in bash.

    – mosvy
    Nov 27 '18 at 15:21








  • 1





    Also, some shells like zsh or yash optimise out a fork() for the last command in a subshell. They may even optimise out the fork for the subshell if it's the last command in a script so your getpid could even report the parent of $$. You could define getpid as: getpid(){ sh -c 'echo "$PPID"'; return; } to disable avoid the problem.

    – Stéphane Chazelas
    Nov 27 '18 at 16:01



















14














$ echo $BASHPID
37152
$ ( echo $BASHPID )
18633


From the manual:




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.







$



Expands to the process ID of the shell. In a () subshell, it
expands to the process ID of the current shell, not the
subshell.




Related:





  • Do parentheses really put the command in a subshell?, especially parts of Gilles' answer.






share|improve this answer


























  • Thanks. (1) What does "re-initialized" mean? (2) Could you also consider why those ways I have tried do not work?

    – Tim
    Nov 27 '18 at 13:36













  • @Tim I believe this is answered by Gilles here. Bash simply does not update $$ in subshells.

    – Kusalananda
    Nov 27 '18 at 13:44











  • Do you mean I should always use $BASHPID in place of $$ in any case in bash? When shall I use which?

    – Tim
    Nov 27 '18 at 14:09













  • @Tim It depends on whether you, in a subshell, wants to get the process ID of the script or of the subshell. Both possibilities are provided and which is the correct one is dependent on the application. No more specific answer can be given to that.

    – Kusalananda
    Nov 27 '18 at 14:20






  • 1





    @Tim The PID of a parent shell of a subshell can't reliably be found unless you arrange to save $BASHPID in a variable and use that in the subshell. There is $PPID, but that's the parent PID of the shell in the same sense that $$ is the PID of the shell (it's not reset in a subshell). There is no $BASHPPID variable.

    – Kusalananda
    Nov 27 '18 at 14:57














Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f484442%2fhow-can-i-get-the-pid-of-a-subshell%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









7














In addition to bash's $BASHPID, you can do it portably with:



pid=$(exec sh -c 'echo "$PPID"')


Example:



(pid=$(exec sh -c 'echo "$PPID"'); echo "$$ $pid")


You can make it into a function:



# usage getpid [varname]
getpid(){
pid=$(exec sh -c 'echo "$PPID"')
test "$1" && eval "$1=$pid"
}


Notice that some shells (eg. zsh or ksh93) do NOT start a subprocess for each subshell created with (...); in that case, $pid may be end up being the same as $$, which is just right, because that's the PID of the process getpid was called from.






share|improve this answer


























  • Thanks. By portably, you mean ...?

    – Tim
    Nov 27 '18 at 15:13











  • Should work in any POSIX shell -- eg in debian's /bin/sh (dash) and in busybox (ash).

    – mosvy
    Nov 27 '18 at 15:15






  • 1





    No. But please do not assume that a subshell is necessarily run in a subprocess -- that is not the case in ksh93, for instance.

    – mosvy
    Nov 27 '18 at 15:18








  • 1





    It will work fine in ksh93 -- it will always return the pid of the process it was called from. It's the (...) from the example which may not spawn a separate process, as it does in bash.

    – mosvy
    Nov 27 '18 at 15:21








  • 1





    Also, some shells like zsh or yash optimise out a fork() for the last command in a subshell. They may even optimise out the fork for the subshell if it's the last command in a script so your getpid could even report the parent of $$. You could define getpid as: getpid(){ sh -c 'echo "$PPID"'; return; } to disable avoid the problem.

    – Stéphane Chazelas
    Nov 27 '18 at 16:01
















7














In addition to bash's $BASHPID, you can do it portably with:



pid=$(exec sh -c 'echo "$PPID"')


Example:



(pid=$(exec sh -c 'echo "$PPID"'); echo "$$ $pid")


You can make it into a function:



# usage getpid [varname]
getpid(){
pid=$(exec sh -c 'echo "$PPID"')
test "$1" && eval "$1=$pid"
}


Notice that some shells (eg. zsh or ksh93) do NOT start a subprocess for each subshell created with (...); in that case, $pid may be end up being the same as $$, which is just right, because that's the PID of the process getpid was called from.






share|improve this answer


























  • Thanks. By portably, you mean ...?

    – Tim
    Nov 27 '18 at 15:13











  • Should work in any POSIX shell -- eg in debian's /bin/sh (dash) and in busybox (ash).

    – mosvy
    Nov 27 '18 at 15:15






  • 1





    No. But please do not assume that a subshell is necessarily run in a subprocess -- that is not the case in ksh93, for instance.

    – mosvy
    Nov 27 '18 at 15:18








  • 1





    It will work fine in ksh93 -- it will always return the pid of the process it was called from. It's the (...) from the example which may not spawn a separate process, as it does in bash.

    – mosvy
    Nov 27 '18 at 15:21








  • 1





    Also, some shells like zsh or yash optimise out a fork() for the last command in a subshell. They may even optimise out the fork for the subshell if it's the last command in a script so your getpid could even report the parent of $$. You could define getpid as: getpid(){ sh -c 'echo "$PPID"'; return; } to disable avoid the problem.

    – Stéphane Chazelas
    Nov 27 '18 at 16:01














7












7








7







In addition to bash's $BASHPID, you can do it portably with:



pid=$(exec sh -c 'echo "$PPID"')


Example:



(pid=$(exec sh -c 'echo "$PPID"'); echo "$$ $pid")


You can make it into a function:



# usage getpid [varname]
getpid(){
pid=$(exec sh -c 'echo "$PPID"')
test "$1" && eval "$1=$pid"
}


Notice that some shells (eg. zsh or ksh93) do NOT start a subprocess for each subshell created with (...); in that case, $pid may be end up being the same as $$, which is just right, because that's the PID of the process getpid was called from.






share|improve this answer















In addition to bash's $BASHPID, you can do it portably with:



pid=$(exec sh -c 'echo "$PPID"')


Example:



(pid=$(exec sh -c 'echo "$PPID"'); echo "$$ $pid")


You can make it into a function:



# usage getpid [varname]
getpid(){
pid=$(exec sh -c 'echo "$PPID"')
test "$1" && eval "$1=$pid"
}


Notice that some shells (eg. zsh or ksh93) do NOT start a subprocess for each subshell created with (...); in that case, $pid may be end up being the same as $$, which is just right, because that's the PID of the process getpid was called from.







share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 2 '18 at 0:29

























answered Nov 27 '18 at 15:00









mosvymosvy

14.1k21547




14.1k21547













  • Thanks. By portably, you mean ...?

    – Tim
    Nov 27 '18 at 15:13











  • Should work in any POSIX shell -- eg in debian's /bin/sh (dash) and in busybox (ash).

    – mosvy
    Nov 27 '18 at 15:15






  • 1





    No. But please do not assume that a subshell is necessarily run in a subprocess -- that is not the case in ksh93, for instance.

    – mosvy
    Nov 27 '18 at 15:18








  • 1





    It will work fine in ksh93 -- it will always return the pid of the process it was called from. It's the (...) from the example which may not spawn a separate process, as it does in bash.

    – mosvy
    Nov 27 '18 at 15:21








  • 1





    Also, some shells like zsh or yash optimise out a fork() for the last command in a subshell. They may even optimise out the fork for the subshell if it's the last command in a script so your getpid could even report the parent of $$. You could define getpid as: getpid(){ sh -c 'echo "$PPID"'; return; } to disable avoid the problem.

    – Stéphane Chazelas
    Nov 27 '18 at 16:01



















  • Thanks. By portably, you mean ...?

    – Tim
    Nov 27 '18 at 15:13











  • Should work in any POSIX shell -- eg in debian's /bin/sh (dash) and in busybox (ash).

    – mosvy
    Nov 27 '18 at 15:15






  • 1





    No. But please do not assume that a subshell is necessarily run in a subprocess -- that is not the case in ksh93, for instance.

    – mosvy
    Nov 27 '18 at 15:18








  • 1





    It will work fine in ksh93 -- it will always return the pid of the process it was called from. It's the (...) from the example which may not spawn a separate process, as it does in bash.

    – mosvy
    Nov 27 '18 at 15:21








  • 1





    Also, some shells like zsh or yash optimise out a fork() for the last command in a subshell. They may even optimise out the fork for the subshell if it's the last command in a script so your getpid could even report the parent of $$. You could define getpid as: getpid(){ sh -c 'echo "$PPID"'; return; } to disable avoid the problem.

    – Stéphane Chazelas
    Nov 27 '18 at 16:01

















Thanks. By portably, you mean ...?

– Tim
Nov 27 '18 at 15:13





Thanks. By portably, you mean ...?

– Tim
Nov 27 '18 at 15:13













Should work in any POSIX shell -- eg in debian's /bin/sh (dash) and in busybox (ash).

– mosvy
Nov 27 '18 at 15:15





Should work in any POSIX shell -- eg in debian's /bin/sh (dash) and in busybox (ash).

– mosvy
Nov 27 '18 at 15:15




1




1





No. But please do not assume that a subshell is necessarily run in a subprocess -- that is not the case in ksh93, for instance.

– mosvy
Nov 27 '18 at 15:18







No. But please do not assume that a subshell is necessarily run in a subprocess -- that is not the case in ksh93, for instance.

– mosvy
Nov 27 '18 at 15:18






1




1





It will work fine in ksh93 -- it will always return the pid of the process it was called from. It's the (...) from the example which may not spawn a separate process, as it does in bash.

– mosvy
Nov 27 '18 at 15:21







It will work fine in ksh93 -- it will always return the pid of the process it was called from. It's the (...) from the example which may not spawn a separate process, as it does in bash.

– mosvy
Nov 27 '18 at 15:21






1




1





Also, some shells like zsh or yash optimise out a fork() for the last command in a subshell. They may even optimise out the fork for the subshell if it's the last command in a script so your getpid could even report the parent of $$. You could define getpid as: getpid(){ sh -c 'echo "$PPID"'; return; } to disable avoid the problem.

– Stéphane Chazelas
Nov 27 '18 at 16:01





Also, some shells like zsh or yash optimise out a fork() for the last command in a subshell. They may even optimise out the fork for the subshell if it's the last command in a script so your getpid could even report the parent of $$. You could define getpid as: getpid(){ sh -c 'echo "$PPID"'; return; } to disable avoid the problem.

– Stéphane Chazelas
Nov 27 '18 at 16:01













14














$ echo $BASHPID
37152
$ ( echo $BASHPID )
18633


From the manual:




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.







$



Expands to the process ID of the shell. In a () subshell, it
expands to the process ID of the current shell, not the
subshell.




Related:





  • Do parentheses really put the command in a subshell?, especially parts of Gilles' answer.






share|improve this answer


























  • Thanks. (1) What does "re-initialized" mean? (2) Could you also consider why those ways I have tried do not work?

    – Tim
    Nov 27 '18 at 13:36













  • @Tim I believe this is answered by Gilles here. Bash simply does not update $$ in subshells.

    – Kusalananda
    Nov 27 '18 at 13:44











  • Do you mean I should always use $BASHPID in place of $$ in any case in bash? When shall I use which?

    – Tim
    Nov 27 '18 at 14:09













  • @Tim It depends on whether you, in a subshell, wants to get the process ID of the script or of the subshell. Both possibilities are provided and which is the correct one is dependent on the application. No more specific answer can be given to that.

    – Kusalananda
    Nov 27 '18 at 14:20






  • 1





    @Tim The PID of a parent shell of a subshell can't reliably be found unless you arrange to save $BASHPID in a variable and use that in the subshell. There is $PPID, but that's the parent PID of the shell in the same sense that $$ is the PID of the shell (it's not reset in a subshell). There is no $BASHPPID variable.

    – Kusalananda
    Nov 27 '18 at 14:57


















14














$ echo $BASHPID
37152
$ ( echo $BASHPID )
18633


From the manual:




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.







$



Expands to the process ID of the shell. In a () subshell, it
expands to the process ID of the current shell, not the
subshell.




Related:





  • Do parentheses really put the command in a subshell?, especially parts of Gilles' answer.






share|improve this answer


























  • Thanks. (1) What does "re-initialized" mean? (2) Could you also consider why those ways I have tried do not work?

    – Tim
    Nov 27 '18 at 13:36













  • @Tim I believe this is answered by Gilles here. Bash simply does not update $$ in subshells.

    – Kusalananda
    Nov 27 '18 at 13:44











  • Do you mean I should always use $BASHPID in place of $$ in any case in bash? When shall I use which?

    – Tim
    Nov 27 '18 at 14:09













  • @Tim It depends on whether you, in a subshell, wants to get the process ID of the script or of the subshell. Both possibilities are provided and which is the correct one is dependent on the application. No more specific answer can be given to that.

    – Kusalananda
    Nov 27 '18 at 14:20






  • 1





    @Tim The PID of a parent shell of a subshell can't reliably be found unless you arrange to save $BASHPID in a variable and use that in the subshell. There is $PPID, but that's the parent PID of the shell in the same sense that $$ is the PID of the shell (it's not reset in a subshell). There is no $BASHPPID variable.

    – Kusalananda
    Nov 27 '18 at 14:57
















14












14








14







$ echo $BASHPID
37152
$ ( echo $BASHPID )
18633


From the manual:




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.







$



Expands to the process ID of the shell. In a () subshell, it
expands to the process ID of the current shell, not the
subshell.




Related:





  • Do parentheses really put the command in a subshell?, especially parts of Gilles' answer.






share|improve this answer















$ echo $BASHPID
37152
$ ( echo $BASHPID )
18633


From the manual:




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.







$



Expands to the process ID of the shell. In a () subshell, it
expands to the process ID of the current shell, not the
subshell.




Related:





  • Do parentheses really put the command in a subshell?, especially parts of Gilles' answer.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 27 '18 at 13:42

























answered Nov 27 '18 at 13:34









KusalanandaKusalananda

151k18298478




151k18298478













  • Thanks. (1) What does "re-initialized" mean? (2) Could you also consider why those ways I have tried do not work?

    – Tim
    Nov 27 '18 at 13:36













  • @Tim I believe this is answered by Gilles here. Bash simply does not update $$ in subshells.

    – Kusalananda
    Nov 27 '18 at 13:44











  • Do you mean I should always use $BASHPID in place of $$ in any case in bash? When shall I use which?

    – Tim
    Nov 27 '18 at 14:09













  • @Tim It depends on whether you, in a subshell, wants to get the process ID of the script or of the subshell. Both possibilities are provided and which is the correct one is dependent on the application. No more specific answer can be given to that.

    – Kusalananda
    Nov 27 '18 at 14:20






  • 1





    @Tim The PID of a parent shell of a subshell can't reliably be found unless you arrange to save $BASHPID in a variable and use that in the subshell. There is $PPID, but that's the parent PID of the shell in the same sense that $$ is the PID of the shell (it's not reset in a subshell). There is no $BASHPPID variable.

    – Kusalananda
    Nov 27 '18 at 14:57





















  • Thanks. (1) What does "re-initialized" mean? (2) Could you also consider why those ways I have tried do not work?

    – Tim
    Nov 27 '18 at 13:36













  • @Tim I believe this is answered by Gilles here. Bash simply does not update $$ in subshells.

    – Kusalananda
    Nov 27 '18 at 13:44











  • Do you mean I should always use $BASHPID in place of $$ in any case in bash? When shall I use which?

    – Tim
    Nov 27 '18 at 14:09













  • @Tim It depends on whether you, in a subshell, wants to get the process ID of the script or of the subshell. Both possibilities are provided and which is the correct one is dependent on the application. No more specific answer can be given to that.

    – Kusalananda
    Nov 27 '18 at 14:20






  • 1





    @Tim The PID of a parent shell of a subshell can't reliably be found unless you arrange to save $BASHPID in a variable and use that in the subshell. There is $PPID, but that's the parent PID of the shell in the same sense that $$ is the PID of the shell (it's not reset in a subshell). There is no $BASHPPID variable.

    – Kusalananda
    Nov 27 '18 at 14:57



















Thanks. (1) What does "re-initialized" mean? (2) Could you also consider why those ways I have tried do not work?

– Tim
Nov 27 '18 at 13:36







Thanks. (1) What does "re-initialized" mean? (2) Could you also consider why those ways I have tried do not work?

– Tim
Nov 27 '18 at 13:36















@Tim I believe this is answered by Gilles here. Bash simply does not update $$ in subshells.

– Kusalananda
Nov 27 '18 at 13:44





@Tim I believe this is answered by Gilles here. Bash simply does not update $$ in subshells.

– Kusalananda
Nov 27 '18 at 13:44













Do you mean I should always use $BASHPID in place of $$ in any case in bash? When shall I use which?

– Tim
Nov 27 '18 at 14:09







Do you mean I should always use $BASHPID in place of $$ in any case in bash? When shall I use which?

– Tim
Nov 27 '18 at 14:09















@Tim It depends on whether you, in a subshell, wants to get the process ID of the script or of the subshell. Both possibilities are provided and which is the correct one is dependent on the application. No more specific answer can be given to that.

– Kusalananda
Nov 27 '18 at 14:20





@Tim It depends on whether you, in a subshell, wants to get the process ID of the script or of the subshell. Both possibilities are provided and which is the correct one is dependent on the application. No more specific answer can be given to that.

– Kusalananda
Nov 27 '18 at 14:20




1




1





@Tim The PID of a parent shell of a subshell can't reliably be found unless you arrange to save $BASHPID in a variable and use that in the subshell. There is $PPID, but that's the parent PID of the shell in the same sense that $$ is the PID of the shell (it's not reset in a subshell). There is no $BASHPPID variable.

– Kusalananda
Nov 27 '18 at 14:57







@Tim The PID of a parent shell of a subshell can't reliably be found unless you arrange to save $BASHPID in a variable and use that in the subshell. There is $PPID, but that's the parent PID of the shell in the same sense that $$ is the PID of the shell (it's not reset in a subshell). There is no $BASHPPID variable.

– Kusalananda
Nov 27 '18 at 14:57




















draft saved

draft discarded




















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f484442%2fhow-can-i-get-the-pid-of-a-subshell%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

Nicolae Petrescu-Găină Cuprins Biografie | Opera | In memoriam | Varia | Controverse, incertitudini...